Java基础--集合框架之Set

本文详细介绍了 Java 中 Set 集合的特点与使用方法,包括 HashSet 和 TreeSet 的内部实现原理,以及如何利用自定义对象进行排序。通过具体实例展示了如何重写 hashCode 和 equals 方法确保元素唯一性,并介绍了两种 TreeSet 排序方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


凌风博客原创作品。转载请注明出处:http://blog.youkuaiyun.com/q549130180/article/details/45313445

|--Set:元素是无序(存入和取出的顺序不一定一致),元素不可以重复,线程是非同步的

|--HashSet:底层数据结构式哈希表
HashSet是如何保证元素唯一性的呢?
是通过元素的两个方法,hashCode和equals来完成
如果元素的HashCode值相同,才会判断equals是否为true
如果元素的Hashcode值不同,才会调用equals


注意:对于判断元素是否存在,以及删除等操作,依赖的方法是元素的hashcode和equals方法


|--TreeSet:可以对Set集合中的元素进行排序
底层数据结构式二叉树
保证元素唯一性的依据
compareTo方法return 0


TreeSet排序的第一种方式:让元素自身具备比较性
元素需要实现Comparable接口,覆盖compareTo方法
这种方式也称为元素的自然顺序,或者叫做默认顺序


TreeSet的第二种排序方式
当元素自身不具备比较性时,或者具备的比较性不是所需要的
这时就需要让集合自身具备比较性
在集合初始化时,就有了比较方式


Set集合的功能和Collection是一致的。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. class HashSetDemo1  
  2. {  
  3.     public static void sop(Object obj)  
  4.     {  
  5.         System.out.println(obj);  
  6.     }  
  7.   
  8.     public static void main(String[] args)   
  9.     {  
  10.         HashSet hs = new HashSet();  
  11.   
  12.         hs.add(new Person("a1",11));  
  13.         hs.add(new Person("a2",12));  
  14.         hs.add(new Person("a3",13));  
  15.         hs.add(new Person("a2",12));  
  16.   
  17.           
  18.         sop("a1:"+hs.contains(new Person("a2",12)));  
  19.         //hs.remove(new Person("a3",13));  
  20.   
  21.         Iterator it = new iterator();  
  22.         while (it.hasNext)  
  23.         {  
  24.             Person p = (Person)it.next();  
  25.             sop(p.getName()+"::"+p.getAge());  
  26.         }  
  27.     }  
  28. }  
  29.   
  30. class Person  
  31. {  
  32.     Person(String name,int age)  
  33.     {  
  34.         this.name = name;  
  35.         this.age = age;  
  36.     }  
  37.   
  38.     public int hashCode()  
  39.     {  
  40.         System.out.println(this.name+"....hashCode");  
  41.   
  42.         return name.hashCode()+age;  
  43.     }  
  44.   
  45.     public boolean equals(Object obj)  
  46.     {  
  47.         if (!(obj instanceof Person))  
  48.         {  
  49.             return false;  
  50.         }  
  51.         System.out.println(this.name+"..."+p.name);  
  52.   
  53.         return this.name.equals(p.name) && this.age ==p.age;  
  54.     }  
  55.   
  56.     public String getName()  
  57.     {  
  58.         return name;  
  59.     }  
  60.   
  61.     public int getAge()  
  62.     {  
  63.         return age;  
  64.     }  
  65. }  

需求:
往TreeSet集合中存储自定义对象学生
想按照学生的年龄进行排序


记住:排序时,当主要条件相同时,一定判断一下次要条件

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. class TreeSetDemo2   
  2. {  
  3.     public static void main(String[] args)   
  4.     {  
  5.         TreeSet ts = new TreeSet();  
  6.   
  7.         ts.add(new Studet("lisi02",22));  
  8.         ts.add(new Studet("lisi07",20));  
  9.         ts.add(new Studet("lisi09",19));  
  10.         ts.add(new Studet("lisi08",19));  
  11.         ts.add(new Studet("lisi07",20));  
  12.   
  13.         Iterator it = ts.iterator();  
  14.   
  15.         while (it.hasNext())  
  16.         {  
  17.             Studet stu = (Student)it.next();  
  18.             System.out.println(stu.getName()+"..."+stu.get.Age());  
  19.         }  
  20.           
  21.     }  
  22. }  
  23.   
  24. class Student implements Comparable  //该接口强制让学生具备比较性  
  25. {  
  26.     private String name;  
  27.     private int age;  
  28.     Student(String name,int age)  
  29.     {  
  30.         this.name = name;  
  31.         this.age = age;  
  32.     }  
  33.   
  34.     public int compareTo(Object obj)  
  35.     {  
  36.         if (!(obj instanceof Student))  
  37.         {  
  38.             throw new RuntimeException("不是学生对象");  
  39.         }  
  40.   
  41.         Student s = (Student)obj;  
  42.   
  43.         System.out.println(this.name+"..compareto.."+s.name);  
  44.   
  45.         if(this.age>s.age)  
  46.             return 1;  
  47.         if(this.age==s.age)  
  48.             return this.name.compareTo(s.name);  
  49.         return -1;  
  50.     }  
  51.   
  52.     public String getName()  
  53.     {  
  54.         return name;  
  55.     }  
  56.   
  57.     public int getAge()  
  58.     {  
  59.         return age;  
  60.     }  
  61. }  

当元素自身不具备比较性,或者具备的比较性不是所需要的
这时需要让容器自身具备比较性。
定义了比较器,将比较器对象作为参数传递给TreeSet集合的构造函数


当两种排序都存在时,以比较器为主


定义一个类,实现Comparator接口,覆盖compare方法

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. class TreeSetDemo2_1  
  2. {  
  3.     public static void main(String[] args)   
  4.     {  
  5.         TreeSet ts = new TreeSet(new MyCompare());  
  6.   
  7.         ts.add(new Studet("lisi02",22));  
  8.         ts.add(new Studet("lisi07",20));  
  9.         ts.add(new Studet("lisi09",19));  
  10.         ts.add(new Studet("lisi08",19));  
  11.         ts.add(new Studet("lisi07",20));  
  12.   
  13.         Iterator it = ts.iterator();  
  14.   
  15.         while (it.hasNext())  
  16.         {  
  17.             Studet stu = (Student)it.next();  
  18.             System.out.println(stu.getName()+"..."+stu.get.Age());  
  19.         }  
  20.     }  
  21. }  
  22.   
  23. class MyCompare implements Comparator  
  24. {  
  25.     public int compare(Object o1,Object o2)  
  26.     {  
  27.         Student s1 = (Student)o1;  
  28.         Student s2 = (Student)o2;  
  29.   
  30.         int num = s1.getName().compareTo(s2.getName());  
  31.   
  32.         if (num==0)  
  33.         {  
  34.             return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));  
  35.             /* 
  36.             if (s1.getAge()>s2.getAge) 
  37.                 return 1; 
  38.             if (s1.getAge()==s2.getAge()) 
  39.                 return 0; 
  40.             return -1; 
  41.             */  
  42.         }  
  43.         return num;  
  44.   
  45.     }  
  46. }  
  47.   
  48.   
  49.   
  50. class Student implements Comparable  //该接口强制让学生具备比较性  
  51. {  
  52.     private String name;  
  53.     private int age;  
  54.     Student(String name,int age)  
  55.     {  
  56.         this.name = name;  
  57.         this.age = age;  
  58.     }  
  59.   
  60.     public int compareTo(Object obj)  
  61.     {  
  62.         if (!(obj instanceof Student))  
  63.         {  
  64.             throw new RuntimeException("不是学生对象");  
  65.         }  
  66.   
  67.         Student s = (Student)obj;  
  68.   
  69.         System.out.println(this.name+"..compareto.."+s.name);  
  70.   
  71.         if(this.age>s.age)  
  72.             return 1;  
  73.         if(this.age==s.age)  
  74.             return this.name.compareTo(s.name);  
  75.         return -1;  
  76.     }  
  77.   
  78.     public String getName()  
  79.     {  
  80.         return name;  
  81.     }  
  82.   
  83.     public int getAge()  
  84.     {  
  85.         return age;  
  86.     }  
  87. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值