当前位置:网站首页>Set集合
Set集合
2022-06-30 11:53:00 【時宜】
目录
一、特点
特点:无序,不重复
如何去重
先将包含重复元素的list构造出一个HashSet,再通过HashSet构造出一个ArrayList
private List<Integer>list=new ArrayList<>();
@Before
public void setup() {
list.add(1);
list.add(1);
list.add(2);
list.add(2);
list.add(3);
list.add(3);
}
@Test
public void test01() {
List<Integer> tmp=new ArrayList<>(new HashSet<Integer>(list));
System.out.println(tmp);
}效果图如下:

二、遍历
foreach,迭代器
private Set<Integer>set=new HashSet<>();
@Before
public void setup() {
set.add(1);
set.add(2);
set.add(2);
set.add(3);
set.add(4);
set.add(5);
set.add(6);
set.add(7);
}
/**
* 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());
}
}
效果图如下:

三、扩容
扩容: 初始容量16,负载因子0.75,扩容增量1倍
四、实现
HashSet
它存储唯一元素并允许空值,依据对象的hashcode来确定该元素是否存在
示例:
首先建立一个Student的实体类
package com.zking.set;
public class Student {
private Integer sid;
private String sname;
private Integer sage;
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Integer getSage() {
return sage;
}
public void setSage(Integer sage) {
this.sage = sage;
}
public Student(Integer sid, String sname, Integer sage) {
super();
this.sid = sid;
this.sname = sname;
this.sage = sage;
}
public Student() {
super();
}
@Override
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + ", sage=" + sage + "]";
}
}
测试方法
public void test04() {
Set<Student>stu=new HashSet<>();
stu.add(new Student(1,"苡桉",18));
stu.add(new Student(1,"苡桉",18));
stu.add(new Student(2,"千帆",19));
stu.add(new Student(3,"鼠标",17));
stu.add(new Student(4,"耳机",22));
stu.add(new Student(5,"安楠",30));
for (Student s : stu) {
System.out.println(s);
}
}运行结果:发现并没有去重

在实体类里重写hascode以及equals方法后
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((sage == null) ? 0 : sage.hashCode());
result = prime * result + ((sid == null) ? 0 : sid.hashCode());
result = prime * result + ((sname == null) ? 0 : sname.hashCode());
return result;
}
@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 (sage == null) {
if (other.sage != null)
return false;
} else if (!sage.equals(other.sage))
return false;
if (sid == null) {
if (other.sid != null)
return false;
} else if (!sid.equals(other.sid))
return false;
if (sname == null) {
if (other.sname != null)
return false;
} else if (!sname.equals(other.sname))
return false;
return true;
}
效果图如下:去重成功

注意:HashSet不保持插入顺序,非线程安全
性能参数:初始容量,负载因子
- 默认值: 初始容量16,负载因子0.75
- 示例:new HashSet<>(20, 0.5f);
set删除(删除元素)
public void test05() {
set.remove(7);
System.out.println(set);
}
效果图如下:

TreeSet
是一个包含有序的且没有重复元素的集合,作用是提供有序的Set集合,自然排序或者根据提供的Comparator进行排序。TreeSet是基于TreeMap实现的。
示例:如何根据年龄排序(从大到小)
public void test04() {
//自定义比较器
TreeSet<Student>stu=new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o2.getSage()-o1.getSage();
}
});
stu.add(new Student(1,"苡桉",18));
stu.add(new Student(1,"苡桉",18));
stu.add(new Student(2,"千帆",19));
stu.add(new Student(3,"鼠标",17));
stu.add(new Student(4,"耳机",22));
stu.add(new Student(5,"安楠",30));
for (Student s : stu) {
System.out.println(s);
}
}
效果图如下:

删除对象
自定义比较器
通过构造函数传入比较器
public void test04() {
TreeSet<Student>stu=new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o2.getSid()-o1.getSid();
}
});
stu.add(new Student(1,"苡桉",18));
stu.add(new Student(1,"苡桉",18));
stu.add(new Student(2,"千帆",19));
stu.add(new Student(3,"鼠标",17));
stu.add(new Student(7,"鼠标",18));
stu.add(new Student(4,"耳机",22));
stu.add(new Student(5,"安楠",30));
stu.remove(new Student(3,"鼠标",17));
for (Student s : stu) {
System.out.println(s);
}
}效果图如下:

实现排序接口
package com.zking.set;
public class Student implements Comparable<Student>{
private Integer sid;
private String sname;
private Integer sage;
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Integer getSage() {
return sage;
}
public void setSage(Integer sage) {
this.sage = sage;
}
public Student(Integer sid, String sname, Integer sage) {
super();
this.sid = sid;
this.sname = sname;
this.sage = sage;
}
public Student() {
super();
}
@Override
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + ", sage=" + sage + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((sage == null) ? 0 : sage.hashCode());
result = prime * result + ((sid == null) ? 0 : sid.hashCode());
result = prime * result + ((sname == null) ? 0 : sname.hashCode());
return result;
}
@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 (sage == null) {
if (other.sage != null)
return false;
} else if (!sage.equals(other.sage))
return false;
if (sid == null) {
if (other.sid != null)
return false;
} else if (!sid.equals(other.sid))
return false;
if (sname == null) {
if (other.sname != null)
return false;
} else if (!sname.equals(other.sname))
return false;
return true;
}
@Override
public int compareTo(Student o) {
// TODO Auto-generated method stub
return this.getSid()-o.getSid();
}
}
注意:在使用比较器进行删除时要注意比较器中的字段 若删除的数据中有重复的元素 而这个元素如果与比较器的字段一样 那么可能会出现删除错误。 因为比较器会根据你设定的字段排序,如果出现重复会自动进行去重。
public void test04() {
TreeSet<Student>stu=new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o2.getSage()-o1.getSage();
}
});
stu.add(new Student(1,"苡桉",18));
stu.add(new Student(1,"苡桉",18));
stu.add(new Student(2,"千帆",19));
stu.add(new Student(3,"鼠标",17));
stu.add(new Student(7,"鼠标",18));
stu.add(new Student(4,"耳机",22));
stu.add(new Student(5,"安楠",30));
stu.remove(new Student(3,"鼠标",17));
for (Student s : stu) {
System.out.println(s);
}
}效果图入下:会发现想删除的的是 Student(3,"鼠标",17),却把Student(7,"鼠标",18)也删了

解决方案如下:
public void test04() {
TreeSet<Student>stu=new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
if(o1.getSage()-o2.getSage()==0) {
return o1.getSid()-o2.getSid();
}
return o1.getSage()-o2.getSage();
}
});
//stu.add(new Student(1,"苡桉",18));
stu.add(new Student(1,"苡桉",18));
stu.add(new Student(2,"千帆",19));
stu.add(new Student(3,"鼠标",17));
stu.add(new Student(7,"鼠标",18));
stu.add(new Student(4,"耳机",22));
stu.add(new Student(5,"安楠",30));
stu.remove(new Student(3,"鼠标",17));
for (Student s : stu) {
System.out.println(s);
}
}效果图如下:
边栏推荐
- R语言ggplot2可视化:使用ggplot2可视化散点图、在geom_point参数中设置show_legend参数为FALSE配置不显示图例信息
- Embedded SIG | 多 OS 混合部署框架
- shell第一个命令结果传入第二个命令删除
- STM32F407ZGT6使用SDIO方式驱动SD卡
- Speech recognition - Fundamentals (I): introduction [speech to text]
- [cf] 803 div2 B. Rising Sand
- STM32 porting the fish component of RT thread Standard Edition
- Who still remembers "classmate Zhang"?
- [revisiting the classic C language] ~x,%c,%d,%x, etc. in C language, the role of the address character in C language, and the consortium in C language
- lvgl 小部件样式篇
猜你喜欢

A Generic Deep-Learning-Based Approach for Automated Surface Inspection-論文閱讀筆記

STM32F407ZGT6使用SDIO方式驱动SD卡

基于视觉的机器人抓取:从物体定位、物体姿态估计到平行抓取器抓取估计

Boost study: boost log

Redis6学习笔记-第二章-Redis6的基本操作

他是上海两大产业的第一功臣,却在遗憾中默默离世

Beego development blog system learning (II)
Redis - ziplist compressed list
![[pattern recognition]](/img/b1/dcb444cbf40a43eeb7f7b233d7741a.png)
[pattern recognition]

Embedded SIG | 多 OS 混合部署框架
随机推荐
线下门店为什么要做新零售?
STM32 移植 RT-Thread 标准版的 FinSH 组件
led背光板的作用是什麼呢?
Redis - ziplist compressed list
ModelAtrs声音检测,基于声学特征的异响检测
一个悄然崛起的国产软件,低调又强大!
Redis6学习笔记-第二章-Redis6的基本操作
A high precision positioning approach for category support components with multiscale difference reading notes
R语言ggplot2可视化分面图(facet):gganimate包基于transition_time函数创建动态散点图动画(gif)、使用labs函数为动画图添加动态时间标题
自定义一个注解来获取数据库的链接
R language ggplot2 visualization: gganimate package is based on Transition_ The time function creates a dynamic scatter graph animation (GIF), and uses the labs function to add a dynamic time title to
200. number of islands
In depth analysis of Apache bookkeeper series: Part 4 - back pressure
Time function and clock_ Differences between gettime() functions
R语言ggplot2可视化:使用ggplot2可视化散点图、使用scale_color_viridis_d函数指定数据点的配色方案
Openmldb meetup No.4 meeting minutes
来聊聊怎么做硬件兼容性检测,快速迁移到openEuler?
Embedded SIG | 多 OS 混合部署框架
AUTOCAD——LEN命令
MySQL索引和优化的理解学习