前言:本篇文章与上一篇文章UML图与List集合有关系哟~,建议一起看看(☞゚ヮ゚)☞https://blog.csdn.net/weixin_64938628/article/details/125486950?spm=1001.2014.3001.5502
目录
特点:
1.无序性:set集合是无序的
2.不重复:set集合是不可以重复的
遍历方法:
foreach,迭代器
扩容: 初始容量16,负载因子0.75,扩容增量1倍
Set和List一样属于接口,无法直接创建实例化对象,需要实现类来set接口。
关系图

HashSet实现Set接口,底层由HashMap来实现,为哈希表结构,新增元素相当于HashMap的key,value默认为一个固定的Object。
当有元素插入的时候,会计算元素的hashCode值,将元素插入到哈希表对应的位置中来;
它继承于AbstractSet,实现了Set, Cloneable, Serializable接口。
(1)HashSet继承AbstractSet类,获得了Set接口大部分的实现,减少了实现此接口所需的工作,实际上是又继承了AbstractCollection类;
(2)HashSet实现了Set接口,获取Set接口的方法,可以自定义具体实现,也可以继承AbstractSet类中的实现;
(3)HashSet实现Cloneable,得到了clone()方法,可以实现克隆功能;
(4)HashSet实现Serializable,表示可以被序列化,通过序列化去传输,典型的应用就是hessian协议。
具有如下特点:
不允许出现重复因素;
允许插入Null值;
元素无序(添加顺序和遍历顺序不一致);
线程不安全,若2个线程同时操作HashSet,必须通过代码实现同步;
这里我们可以思考一下如果对List容器中的元素去重?),可以使用HashSet呦~
这里我就浅浅为伙伴们举一个例子:
- public class SetDemo {
-
- private List<Integer> list = new ArrayList<>();
-
- @BeforeAll
- public void setup() {
- set.add(1);
- set.add(1);
- set.add(2);
- set.add(2);
- set.add(3);
- set.add(3);
- }
-
- @Test
- public void test01() {
- List<Integer> tmp = new ArrayList<>(new HashSet<Integer>(list));
- System.out.println(tmp);
- }
- }
这里要注意哦,前面说了set集合是不可以重复的,但是ArrayList是可以存放重复元素的。一定要注意区分哦👀
最后输出的是123。
为了更好的理解,我在这里再举一个例子,首先我们新建一个Set集合放入元素
- public class SetDemo {
-
- private Set<Integer> set = new HashSet<>();
-
- @BeforeAll
- public void setup() {
- set.add(1);
- set.add(1);
- set.add(2);
- set.add(4);
- set.add(5);
- set.add(3);
- }
- }
然后我会分别使用foreach和迭代器来遍历
- //foreach
- @Test
- public void test02() {
- for(Integer e: set) {
- System.out.println(e);
- }
- }
-
- //使用迭代器
- @Test
- public void test03() {
- Iterator<Integer> it = set.iterator();
- while(it.hasNext()) {
- System.out.println(it.next());
- }
- }
由于HashSet中只能存储不重复的对象,所以输出时会自动把重复的元素去重。
从名字上可以看出,此集合的实现和树结构有关。与HashSet集合类似,TreeSet也是基于Map来实现,具体实现TreeMap,其底层结构为红黑树(特殊的二叉查找树);
与HashSet不同的是,TreeSet具有排序功能,分为自然排序(123456)和自定义排序两类,默认是自然排序;在程序中,我们可以按照任意顺序将元素插入到集合中,等到遍历时TreeSet会按照一定顺序输出--倒序或者升序;
它继承AbstractSet,实现NavigableSet, Cloneable, Serializable接口。
(1)与HashSet同理,TreeSet继承AbstractSet类,获得了Set集合基础实现操作;
(2)TreeSet实现NavigableSet接口,而NavigableSet又扩展了SortedSet接口。这两个接口主要定义了搜索元素的能力,例如给定某个元素,查找该集合中比给定元素大于、小于、等于的元素集合,或者比给定元素大于、小于、等于的元素个数;简单地说,实现NavigableSet接口使得TreeSet具备了元素搜索功能;
(3)TreeSet实现Cloneable接口,意味着它也可以被克隆;
(4)TreeSet实现了Serializable接口,可以被序列化,可以使用hessian协议来传输;
具有如下特点:
对插入的元素进行排序,是一个有序的集合(主要与HashSet的区别);
底层使用红黑树结构,而不是哈希表结构;
允许插入Null值;
不允许插入重复元素;
线程不安全;
同样,这里我也会写一个例子,首先我们新建一个学生类(随便什么都行)来实现
- package com.zking.list;
-
- public class Student implements Comparable<Student>{
-
- private Integer id;
- private String name;
- private int age;
-
- /**
- * get和set方法
- * @param id
- */
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- 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;
- }
-
- /**
- * hashCode()方法
- */
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + age;
- result = prime * result + ((id == null) ? 0 : id.hashCode());
- result = prime * result + ((name == null) ? 0 : name.hashCode());
- return result;
- }
-
- /**
- * equals()方法
- */
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- Student other = (Student) obj;
- if (age != other.age)
- return false;
- if (id == null) {
- if (other.id != null)
- return false;
- } else if (!id.equals(other.id))
- return false;
- if (name == null) {
- if (other.name != null)
- return false;
- } else if (!name.equals(other.name))
- return false;
- return true;
- }
-
- @Override
- public String toString() {
- return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
- }
-
- public Student(Integer id, String name, int age) {
- super();
- this.id = id;
- this.name = name;
- this.age = age;
- }
-
-
- public int compareTo(Student o) {
- if(this.getAge()-o.getAge()==0) {
- return this.getId()-o.getId();
- }
- return this.getAge() - o.getAge();
- }
-
-
-
-
- }
记得一定要写hashCode()和equals()方法😖
然后再使用ThreeSet
- private Set<Integer> set = new HashSet<>();
-
- @Test
- public void test01() {
- TreeSet<Student> stu = new TreeSet<>();
-
- stu.add(new Student(1,"莉莉", 18));
- stu.add(new Student(1,"莉莉", 18));
- stu.add(new Student(2,"嘿嘿", 19));
- stu.add(new Student(4,"大壮", 10));
- stu.add(new Student(7,"小米", 18));
- stu.add(new Student(5,"妮妮", 20));
- stu.add(new Student(3,"帆帆", 30));
-
- for(Student s: stu) {
- System.out.println(s);
- }
- }
运行结果:

好了,这篇文章就到这里结束了,拜拜。
