当前位置:网站首页>Set set
Set set
2022-06-30 12:09:00 【Timely】
Catalog
3、 ... and 、 Capacity expansion
Pass the comparer through the constructor
One 、 characteristic
characteristic : disorder , No repetition
How to go heavy
The list Construct a HashSet, Re pass HashSet Construct a 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);
}
The renderings are as follows :
Two 、 Traverse
foreach, iterator
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 Traverse
*/
@Test
public void test02() {
for (Integer e : set) {
System.out.println(e);
}
}
/**
* iterator
*/
@Test
public void test03() {
Iterator<Integer> it = set.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
The renderings are as follows :
3、 ... and 、 Capacity expansion
Capacity expansion : Initial capacity 16, Load factor 0.75, Expansion increment 1 times
Four 、 Realization
HashSet
It stores unique elements and allows null values , According to the hashcode To determine whether the element exists
Example :
So let's set up a Student The entity class
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 + "]";
}
}
The test method
public void test04() {
Set<Student>stu=new HashSet<>();
stu.add(new Student(1," Eucalyptus Coicis ",18));
stu.add(new Student(1," Eucalyptus Coicis ",18));
stu.add(new Student(2," Thousand sails ",19));
stu.add(new Student(3," mouse ",17));
stu.add(new Student(4," The headset ",22));
stu.add(new Student(5," An Nan ",30));
for (Student s : stu) {
System.out.println(s);
}
}
Running results : Found no weight loss
Override in entity class hascode as well as equals After the method
@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;
}
The renderings are as follows : Go back to success
Be careful :HashSet The insertion order is not maintained , Non-thread safety
performance parameter : Initial capacity , Load factor
- The default value is : Initial capacity 16, Load factor 0.75
- Example :new HashSet<>(20, 0.5f);
set Delete ( Remove elements )
public void test05() {
set.remove(7);
System.out.println(set);
}
The renderings are as follows :
TreeSet
Is a collection containing ordered elements without repetition , The function is to provide orderly Set aggregate , Sort naturally or according to what is provided Comparator Sort .TreeSet Is based on TreeMap Realized .
Example : How to sort by age ( From big to small )
public void test04() {
// Custom comparator
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," Eucalyptus Coicis ",18));
stu.add(new Student(1," Eucalyptus Coicis ",18));
stu.add(new Student(2," Thousand sails ",19));
stu.add(new Student(3," mouse ",17));
stu.add(new Student(4," The headset ",22));
stu.add(new Student(5," An Nan ",30));
for (Student s : stu) {
System.out.println(s);
}
}
The renderings are as follows :
Delete object
Custom comparator
Pass the comparer through the constructor
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," Eucalyptus Coicis ",18));
stu.add(new Student(1," Eucalyptus Coicis ",18));
stu.add(new Student(2," Thousand sails ",19));
stu.add(new Student(3," mouse ",17));
stu.add(new Student(7," mouse ",18));
stu.add(new Student(4," The headset ",22));
stu.add(new Student(5," An Nan ",30));
stu.remove(new Student(3," mouse ",17));
for (Student s : stu) {
System.out.println(s);
}
}
The renderings are as follows :
Implement sorting interface
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();
}
}
Be careful : When using the comparator to delete, pay attention to the fields in the comparator If there are duplicate elements in the deleted data If this element is the same as the field of the comparator Then a deletion error may occur . Because the comparator will sort according to the fields you set , If there is a repetition, it will be automatically de duplicated .
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," Eucalyptus Coicis ",18));
stu.add(new Student(1," Eucalyptus Coicis ",18));
stu.add(new Student(2," Thousand sails ",19));
stu.add(new Student(3," mouse ",17));
stu.add(new Student(7," mouse ",18));
stu.add(new Student(4," The headset ",22));
stu.add(new Student(5," An Nan ",30));
stu.remove(new Student(3," mouse ",17));
for (Student s : stu) {
System.out.println(s);
}
}
The effect picture goes down : You will find that you want to delete Student(3," mouse ",17), But the Student(7," mouse ",18) Also deleted
The solution is as follows :
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," Eucalyptus Coicis ",18));
stu.add(new Student(1," Eucalyptus Coicis ",18));
stu.add(new Student(2," Thousand sails ",19));
stu.add(new Student(3," mouse ",17));
stu.add(new Student(7," mouse ",18));
stu.add(new Student(4," The headset ",22));
stu.add(new Student(5," An Nan ",30));
stu.remove(new Student(3," mouse ",17));
for (Student s : stu) {
System.out.println(s);
}
}
The renderings are as follows :
边栏推荐
- A Generic Deep-Learning-Based Approach for Automated Surface Inspection-论文阅读笔记
- lvgl 小部件样式篇
- 智慧法院新征程,无纸化办公,护航智慧法院绿色庭审
- Cache avalanche and cache penetration solutions
- Talk about how to do hardware compatibility testing and quickly migrate to openeuler?
- 麒麟软件韩乃平:数字中国建设需要自己的开源根社区
- R language ggplot2 visualization: use ggplot2 visualization scatter diagram and the size parameter in AES function to specify the size of data points (point size)
- 3D视觉检测在生产流水的应用有哪些
- Object mapping - mapping Mapster
- R语言ggplot2可视化:使用ggplot2可视化散点图、在geom_point参数中设置show_legend参数为FALSE配置不显示图例信息
猜你喜欢
Yolov5 export the pit encountered by onnx
Another miserable day by kotlin grammar
led背光板的作用是什么呢?
如何使用插件化机制优雅的封装你的请求hook
Embedded SIG | 多 OS 混合部署框架
移除无效的括号[用数组模拟栈]
OpenMLDB Meetup No.4 会议纪要
NoSQL——Redis的配置与优化
实现多方数据安全共享,解决普惠金融信息不对称难题
wallys/IPQ8074a/2x(4 × 4 or 8 × 8) 11AX MU-MIMO DUAL CONCURRENT EMBEDDEDBOARD
随机推荐
安装onnx很慢,使用清华镜像
Flutter 从零开始 005 图片及Icon
线下门店为什么要做新零售?
Object mapping - mapping Mapster
1254. 统计封闭岛屿的数目
MySQL 复合查询
A quietly rising domestic software, low-key and powerful!
It is said that with this, the boss opened the test overnight
并行接口8255A
YOLOv5导出onnx遇到的坑
R语言ggplot2可视化:使用ggplot2可视化散点图、aes函数中的colour参数指定不同分组的数据点使用不同的颜色显示
Let's talk about how to do hardware compatibility testing and quickly migrate to openeuler?
wallys/3 × 3 MIMO 802.11ac Mini PCIe Wi-Fi Module, QCA9880, 2,4GHz / 5GHzDesigned for Enterprise
ClipboardJS——开发学习总结1
R语言ggplot2可视化:使用ggplot2可视化散点图、使用scale_x_log10函数配置X轴的数值范围为对数坐标
Constructor, class member, destructor call order
go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)
Analysis of KOA - onion model
Beego development blog system learning (II)
这些电影中的科幻构想,已经用AI实现了