
Java集合Set
发布日期:2021-05-07 14:43:36
浏览次数:24
分类:技术文章
本文共 7623 字,大约阅读时间需要 25 分钟。
目录
1 Set集合概述和特点
Set 集合的特点
元素存取无序
没有索引、只能通过迭代器或增强 for 循环遍历
不能存储重复元素
Set 集合的基本使用
public class SetDemo { public static void main(String[] args) {//创建集合对象 Setset = new HashSet ();//添加元素 set.add("hello"); set.add("world"); set.add("java");//不包含重复元素的集合 set.add("world");//遍历 for(String s : set) { System.out.println(s); } }}
2 哈希值
哈希值简介
是 JDK 根据对象的地址或者字符串或者数字算出来的 int 类型的数值
如何获取哈希值
Object 类中的 public int hashCode() :返回对象的哈希码值
哈希值的特点
同一个对象多次调用 hashCode() 方法返回的哈希值是相同的
默认情况下,不同对象的哈希值是不同的。而重写 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 int hashCode() { return 0; }}
测试类
public class HashDemo { public static void main(String[] args) {//创建学生对象 Student s1 = new Student("林青霞",30);//同一个对象多次调用hashCode()方法返回的哈希值是相同的 System.out.println(s1.hashCode()); //1060830840 System.out.println(s1.hashCode()); //1060830840 System.out.println("--------"); Student s2 = new Student("林青霞",30);//默认情况下,不同对象的哈希值是不相同的//通过方法重写,可以实现不同对象的哈希值是相同的 System.out.println(s2.hashCode()); //2137211482 System.out.println("--------"); System.out.println("hello".hashCode()); //99162322 System.out.println("world".hashCode()); //113318802 System.out.println("java".hashCode()); //3254818 System.out.println("world".hashCode()); //113318802 System.out.println("--------"); System.out.println("重地".hashCode()); //1179395 System.out.println("通话".hashCode()); //1179395 }}
3 HashSet集合概述和特点
HashSet 集合的特点
底层数据结构是哈希表
对集合的迭代顺序不作任何保证,也就是说不保证存储和取出的元素顺序一致
没有带索引的方法,所以不能使用普通 for 循环遍历
由于是 Set 集合,所以是不包含重复元素的集合
HashSet 集合的基本使用
public class HashSetDemo01 { public static void main(String[] args) {//创建集合对象 HashSeths = new HashSet ();//添加元素 hs.add("hello"); hs.add("world"); hs.add("java"); hs.add("world");//遍历 for(String s : hs) { System.out.println(s); } }}
4 HashSet集合保证元素唯一性源码分析
HashSet 集合保证元素唯一性的原理:
1. 根据对象的哈希值计算存储位置
- 如果当前位置没有元素则直接存入
- 如果当前位置有元素存在,则进入第二步
2. 当前元素的元素和已经存在的元素比较哈希值
- 如果哈希值不同,则将当前元素进行存储
- 如果哈希值相同,则进入第三步
3. 通过 equals() 方法比较两个元素的内容
- 如果内容不相同,则将当前元素进行存储
- 如果内容相同,则不存储当前元素
HashSet集合保证元素唯一性的图解:
5 常见数据结构之哈希表

6 LinkedHashSet集合概述和特点
LinkedHashSet 集合特点
哈希表和链表实现的 Set 接口,具有可预测的迭代次序
由链表保证元素有序,也就是说元素的存储和取出顺序是一致的
由哈希表保证元素唯一,也就是说没有重复的元素
LinkedHashSet 集合基本使用
public class LinkedHashSetDemo { public static void main(String[] args) {//创建集合对象 LinkedHashSetlinkedHashSet = new LinkedHashSet ();//添加元素 linkedHashSet.add("hello"); linkedHashSet.add("world"); linkedHashSet.add("java"); linkedHashSet.add("world");//遍历集合 for(String s : linkedHashSet) { System.out.println(s); } }}
9 Set集合排序
9.1 TreeSet集合概述和特点
TreeSet 集合概述
元素有序,可以按照一定的规则进行排序,具体排序方式取决于构造方法
TreeSet() :根据其元素的自然排序进行排序
TreeSet(Comparator comparator) :根据指定的比较器进行排序
没有带索引的方法,所以不能使用普通 for 循环遍历
由于是 Set 集合,所以不包含重复元素的集合
TreeSet 集合基本使用
9.2 自然排序Comparable的使用
案例需求
存储学生对象并遍历,创建 TreeSet 集合使用无参构造方法
要求:按照年龄从小到大排序,年龄相同时,按照姓名的字母顺序排序
实现步骤
用 TreeSet 集合存储自定义对象,无参构造方法使用的是自然排序对元素进行排序的
自然排序,就是让元素所属的类实现 Comparable 接口,重写 compareTo(T o) 方法
重写方法时,一定要注意排序规则必须按照要求的主要条件和次要条件来写
代码实现
public class TreeSetDemo01 { public static void main(String[] args) {//创建集合对象 TreeSetts = new TreeSet ();//添加元素 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); } }}
学生类
public class Student implements Comparable{ 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 int compareTo(Student s) {// return 0;// return 1;// return -1;//按照年龄从小到大排序 int num = this.age - s.age;// int num = s.age - this.age;//年龄相同时,按照姓名的字母顺序排序 int num2 = num==0?this.name.compareTo(s.name):num; return num2; }}
测试类
public class TreeSetDemo02 { public static void main(String[] args) {//创建集合对象 TreeSetts = new TreeSet ();//创建学生对象 Student s1 = new Student("xishi", 29); Student s2 = new Student("wangzhaojun", 28); Student s3 = new Student("diaochan", 30); Student s4 = new Student("yangyuhuan", 33); Student s5 = new Student("linqingxia",33); Student s6 = new Student("linqingxia",33);//把学生添加到集合 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()); } }}
9.3 比较器排序Comparator的使用
案例需求
存储学生对象并遍历,创建 TreeSet 集合使用带参构造方法
要求:按照年龄从小到大排序,年龄相同时,按照姓名的字母顺序排序
实现步骤
用 TreeSet 集合存储自定义对象,带参构造方法使用的是比较器排序对元素进行排序的
比较器排序,就是让集合构造方法接收 Comparator 的实现类对象,重写 compare(T o1,T o2) 方法
重写方法时,一定要注意排序规则必须按照要求的主要条件和次要条件来写
代码实现
学生类
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; }}
测试类
public class TreeSetDemo { public static void main(String[] args) {//创建集合对象 TreeSetts = new TreeSet (new Comparator () { @Override public int compare(Student s1, Student s2) {//this.age - s.age//s1,s2 int num = s1.getAge() - s2.getAge(); int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num; return num2; } });//创建学生对象 Student s1 = new Student("xishi", 29); Student s2 = new Student("wangzhaojun", 28); Student s3 = new Student("diaochan", 30); Student s4 = new Student("yangyuhuan", 33); Student s5 = new Student("linqingxia",33); Student s6 = new Student("linqingxia",33);//把学生添加到集合 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()); } }}
10 hashcode和equals的方法
11 HashSet怎么保证元素的唯一性
12 项目实例
也就是原有的equals是比较地址值是否相同来判断对象有无重复,但是要求是名字一样就是一个人要去重,所以要重写equals。
发表评论
最新留言
表示我来过!
[***.240.166.169]2025年03月24日 05时26分27秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
追梦App系列博客——第五次例会总结
2019-03-04
大二数据结构(图的深度遍历的 非递归算法)
2019-03-04
数学建模(NO.18灰色预测)
2019-03-04
数学建模更新14(MATLAB运算)
2019-03-04
数学建模更新12(数学线性规划模型1)
2019-03-04
数学建模更新12(多目标规划)
2019-03-04
Java入门笔记(第三章 类与对象之static静态用法)
2019-03-04
Android,SharedPreferences的使用
2019-03-04
(一)Xshell中给Ubuntu20.04服务器安装mysql并修改密码
2019-03-04
Android中使用ViewPager和Fragment实现底部导航栏
2019-03-04
JAVA_方法的使用(方法重载、方法递归)
2019-03-04
VLAN与Trunk的原理及配置
2019-03-04
三层交换技术及配置
2019-03-04
华为hybrid vlan配置
2019-03-04
OSPF路由重分发配置实例
2019-03-04
BGP实验配置实例
2019-03-04
IEEE期刊缩写(常见的电机控制类期刊)
2019-03-04
VS中使用c++函数显示找不到标识符
2019-03-04
排列组合
2019-03-04
Why Software Development Methodologies Suck?
2019-03-04