Set 集合的应用和概述特点

  1. 不可以存储重复数据
  2. 没有索引,不能使用普通for循环
    public class MySet1 {
        public static void main(String[] args) {
              //创建集合对象
            Set<String> set = new TreeSet<>();
              //添加元素
            set.add("ccc");
            set.add("aaa");
            set.add("aaa");
            set.add("bbb");
    
    //        for (int i = 0; i < set.size(); i++) {
    //            //Set集合是没有索引的,所以不能使用通过索引获取元素的方法
    //        }
    
              //遍历集合
            Iterator<String> it = set.iterator();
            while (it.hasNext()){
                String s = it.next();
                System.out.println(s);
            }
            System.out.println("-----------------------------------");
            for (String s : set) {
                System.out.println(s);
            }
        }
    }

    HashSet集合:

  3. 底层数据结构是哈希表。

  4. 存取无序。

  5. 不可以存储重复元素。

  6. 没有索引,不能使用普通for循环

  7. 总结:HashSet集合存储自定义类型元素,想要实现元素的唯一,要求必须重写hashCode方法。

    public class Student {
        private String name;
        private int age;
    
        public Student() {
        }
    
        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;
        }
    }

    测试类:

  8. public class HashSetDemo02 {
        public static void main(String[] args) {
            //创建HashSet集合对象
            HashSet<Student> hs = new HashSet<Student>();
    
            //创建学生对象
            Student s1 = new Student("林青霞", 30);
            Student s2 = new Student("张曼玉", 35);
            Student s3 = new Student("王祖贤", 33);
    
            Student s4 = new Student("王祖贤", 33);
    
            //把学生添加到集合
            hs.add(s1);
            hs.add(s2);
            hs.add(s3);
            hs.add(s4);
    
            //遍历集合(增强for)
            for (Student s : hs) {
                System.out.println(s.getName() + "," + s.getAge());
            }
        }
    }

    TreeSet

  9. 不可以存储重复元素

  10. 没有索引

  11. 可以将元素按照规则进行排序

  12. 应用:

    public class TreeSetDemo01 {
        public static void main(String[] args) {
            //创建集合对象
            TreeSet<Integer> ts = new TreeSet<Integer>();
    
            //添加元素
            ts.add(10);
            ts.add(40);
            ts.add(30);
            ts.add(50);
            ts.add(20);
    
            ts.add(30);
    
            //遍历集合
            for(Integer i : ts) {
                System.out.println(i);
            }
        }
    }

    自然排序Comparable的使用

  13. 案例需求:* 存储学生对象并遍历,创建TreeSet集合使用无参构造方法
    * 要求:按照年龄从小到大排序,年龄相同时,按照姓名的字母顺序排序

  14. 实现步骤:* 使用空参构造创建TreeSet集合
      * 用TreeSet集合存储自定义对象,无参构造方法使用的是自然排序对元素进行排序的
    * 自定义的Student类实现Comparable接口
      * 自然排序,就是让元素所属的类实现Comparable接口,重写compareTo(T o)方法
    * 重写接口中的compareTo方法
      * 重写方法时,一定要注意排序规则必须按照要求的主要条件和次要条件来写

  15. 代码实现:学生类:

    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;
        }
    
        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 String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    
        @Override
        public int compareTo(Student o) {
            //按照对象的年龄进行排序
            //主要判断条件: 按照年龄从小到大排序
            int result = this.age - o.age;
            //次要判断条件: 年龄相同时,按照姓名的字母顺序排序
            result = result == 0 ? this.name.compareTo(o.getName()) : result;
            return result;
        }
    }

    测试类:

    public class MyTreeSet2 {
        public static void main(String[] args) {
            //创建集合对象
            TreeSet<Student> ts = new TreeSet<>();
            //创建学生对象
            Student s1 = new Student("zhangsan",28);
            Student s2 = new Student("lisi",27);
            Student s3 = new Student("wangwu",29);
            Student s4 = new Student("zhaoliu",28);
            Student s5 = new Student("qianqi",30);
            //把学生添加到集合
            ts.add(s1);
            ts.add(s2);
            ts.add(s3);
            ts.add(s4);
            ts.add(s5);
            //遍历集合
            for (Student student : ts) {
                System.out.println(student);
            }
        }
    }

    比较器排序Comparator的使用

  16. 案例需求:* * 存储老师对象并遍历,创建TreeSet集合使用带参构造方法
      * 要求:按照年龄从小到大排序,年龄相同时,按照姓名的字母顺序排序
    * 实现步骤
      
      * 用TreeSet集合存储自定义对象,带参构造方法使用的是比较器排序对元素进行排序的
      * 比较器排序,就是让集合构造方法接收Comparator的实现类对象,重写compare(T o1,T o2)方法
      * 重写方法时,一定要注意排序规则必须按照要求的主要条件和次要条件来写

  17. 两种比较方式总结:*

  18. 两种比较方式小结
      * 自然排序: 自定义类实现Comparable接口,重写compareTo方法,根据返回值进行排序
      * 比较器排序: 创建TreeSet对象的时候传递Comparator的实现类对象,重写compare方法,根据返回值进行排序
      * 在使用的时候,默认使用自然排序,当自然排序不满足现在的需求时,必须使用比较器排序

  19. 两种方式中关于返回值的规则
      * 如果返回值为负数,表示当前存入的元素是较小值,存左边
      * 如果返回值为0,表示当前存入的元素跟集合中元素重复了,不存
      * 如果返回值为正数,表示当前存入的元素是较大值,存右边

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值