JAVA之路day11

Set集合

LinkedHashSet集合

import java.util.LinkedHashSet;

/*
LinkedHashSet集合特点
    1.哈希表和链表实现的Set接口
    2.由链表保证元素有序,也就是说元素的存储和取出顺序是一致的
    3.由哈希表保证元素唯一。也就是说没有重复的元素。
 */
public class test {
    public static void main(String[] args) {
        //创建集合对象
        LinkedHashSet<String> linkedHashSet=new LinkedHashSet<String>();
        linkedHashSet.add("hello");
        linkedHashSet.add("world");
        linkedHashSet.add("java");
        linkedHashSet.add("java");

        //遍历集合
        for (String s:linkedHashSet){
            System.out.println(s);
        }
    }
}

TreeSet集合

//TreeSet集合概述和特点
/*特点
1.元素有序,这里的顺序不是指存储和去除的顺序,而是按照一定的规则进行排序,具体排序方式取决于构造方法
    TreeSet():根据元素的自然排序进行排序
    TreeSet(Comparator comparator):根据指定的比较器进行排序
2.没有带索引的方法,所以不能用普通for循环。
3.由于是Set集合,所以是不包含重复元素的集合
 */
public class test {
    public static void main(String[] args) {
        //创建一个集合对象
        TreeSet<Integer> ts=new TreeSet<Integer>();

        //增加元素
        ts.add(10);
        ts.add(20);
        ts.add(30);
        ts.add(40);
        ts.add(99);

        //遍历集合
        for (int i:ts){
            System.out.println(i);
        }
    }
}

自然排序comparable的使用

import java.util.TreeSet;

//自然排序Comparable的使用
/*
存储学生对象并遍历,创建TreeSet集合,使用无参构造
要求:按照年龄从小到大排序,年龄相同时,按照姓名的字母顺序排序
 */
/*结论
1.用TreeSet集合存储自定义对象时,无参构造方法使用的是自然排序对元素进行排序的
2.自然排序,就是让元素所属的类实现Comparable接口,重写compareTo(Object 0)方法
3.重写方法时,一定要注意排序规则必须按照要求的主要条件和次要条件来写
 */
public class ComparableTest {
    public static void main(String[] args) {
        //创建集合对象
        TreeSet<Student> ts=new TreeSet<Student>();

        //创建学生对象
        Student s1=new Student("one",1);
        Student s2=new Student("two",2);
        Student s3=new Student("three",3);
        Student s4=new Student("four",4);
        Student s5=new Student("Afour",4);
        Student s6=new Student("Afour",4);
        //添加到集合
        ts.add(s1);
        ts.add(s2);
        ts.add(s3);
        ts.add(s4);
        ts.add(s5);
        ts.add(s6);
        //遍历集合
        for (Student s:ts){
            System.out.println(s.getName()+","+s.getAge());
        }
    }
}
/*
//自定义学生类
public class Student implements Comparable<Student>{
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public int compareTo(Student s) {
        //return 0说明两个对象是一样的
        //return i(正数)  说明比较的对象比次对象小
        //return i(负数)  说明比较的对象比次对象大
        int num=this.age-s.age;
        int num2=num==0?this.name.compareTo(s.name):num;//String类的compareTo方法已经被重写了
        return num2;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
 */

比较器Comparator的使用

import java.util.Comparator;
import java.util.TreeSet;

//Comparator比较器的使用
/*
  存储学生对象并遍历,创建TreeSet集合使用带参构造方法
  要求按照年龄从小到大排序,年龄相同时,按照姓名的字母顺序排序
 */
public class Test {
    public static void main(String[] args) {
        //创建集合对象
        TreeSet<Student> ts=new TreeSet<Student>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                //s1 compareTo s2
                int num=s1.getAge()-s2.getAge();
                int num2=num==0?s1.getName().compareTo(s2.getName()):num;
                return num2;
            }
        });

        //创建对象
        Student s1=new Student("one",1);
        Student s2=new Student("two",2);
        Student s3=new Student("three",3);
        Student s4=new Student("four",4);
        Student s5=new Student("Afour",4);
        Student s6=new Student("Afour",4);

        //添加对象到集合
        ts.add(s1);
        ts.add(s2);
        ts.add(s3);
        ts.add(s4);
        ts.add(s5);
        ts.add(s6);

        //遍历集合
        for (Student s:ts){
            System.out.println(s.getName()+","+s.getAge());
        }
    }
}
/*
//自定义学生类
public class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

 */

案例1 用Tree存储学生对象

import java.util.Comparator;
import java.util.TreeSet;

//案例
/*
需求:用TreeSet集合存储多个学生(姓名,语文成绩,数学成绩),并遍历集合
要求:按照总分从高到低实现
 */
public class Practice {
    public static void main(String[] args) {
        //创建集合
        TreeSet<Student> ts=new TreeSet<Student>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                int num=s1.getSum()-s2.getSum();
                int num2=num==0?s1.getName().compareTo(s2.getName()):num;
                return num2;
            }
        });

        //创建学生对象
        Student s1=new Student("one",1,1);
        Student s2=new Student("two",2,1);
        Student s3=new Student("three",3,1);
        Student s4=new Student("four",4,1);
        Student s5=new Student("Afour",4,1);

        //添加对象到集合
        ts.add(s1);
        ts.add(s2);
        ts.add(s3);
        ts.add(s4);
        ts.add(s5);

        //遍历集合
        for (Student s:ts){
            System.out.println(s.getName()+","+s.getSum());
        }
    }
}
/*
//自定义学生类
public class Student {
    private String name;
    private int chinese;
    private int math;
    public int getSum(){
        return this.chinese+this.math;
    }

    public Student(String name, int chinese, int math) {
        this.name = name;
        this.chinese = chinese;
        this.math = math;
    }

    public Student() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getChinese() {
        return chinese;
    }

    public void setChinese(int chinese) {
        this.chinese = chinese;
    }

    public int getMath() {
        return math;
    }

    public void setMath(int math) {
        this.math = math;
    }
}

 */

案例2 HashSet存储学生对象并遍历

import java.util.HashSet;

//HashSet集合存储学生对象并遍历
/*
需求:创建一个存储对象的集合,存储多个学生对象,使用程序实现在控制台遍历该集合
要求:学生对象的成员变量值相同,我们认为是同一个对象
 */
/*
思路:
1.定义学生类
2.创建HashSet集合对象
3.创建学生对象
4.把学生添加到集合
5.遍历集合
6.在学生类中重写hashCode和equals方法,自动生成即可
 */
public class HsApplication {
    public static void main(String[] args) {
        //创建HashSet集合
        HashSet<Student> hs=new HashSet<Student>();

        //创建学生对象
        Student s1=new Student("one",1);
        Student s2=new Student("two",2);
        Student s3=new Student("three",3);
        Student s4=new Student("three",3);

        //添加进集合
        hs.add(s1);
        hs.add(s2);
        hs.add(s3);
        hs.add(s4);//由于s3和s4是一模一样的,而输出了一样的结果,所以要重写Student类中的HashCode和equals方法

        //遍历集合
        for (Student s:hs){
            System.out.println(s.getName()+","+s.getAge());
        }

    }
}
/*
//用于测试的学生类
public class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;

        if (age != student.age) return false;
        return name != null ? name.equals(student.name) : student.name == null;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + age;
        return result;
    }
}

 */

案例3 不重复的随机数

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

//案例:不重复的随机数
/*思路
1.创建Set对象
2.创建随机数对象
3.判断集合的长度是不是小于10
  是:产生一个随机数添加到集合
4.遍历集合
 */
public class Practice02 {
    public static void main(String[] args) {
        //创建集合
        Set<Integer> run=new HashSet<Integer>();

        //创建随机数对象
         Random i=new Random();

         //判断是否有十个随机数
        while (run.size()<10){
            run.add(i.nextInt(20)+1);
        }

        //遍历集合
        for (int i1:run){
            System.out.println(i1);
        }
    }
}
发布了14 篇原创文章 · 获赞 0 · 访问量 203

猜你喜欢

转载自blog.csdn.net/YSJS99/article/details/105106764