当前位置:网站首页>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 :
边栏推荐
- Flutter 从零开始 006 单选开关和复选框
- NoSQL——Redis的配置与优化
- A Generic Deep-Learning-Based Approach for Automated Surface Inspection-论文阅读笔记
- Map集合
- lvgl 小部件样式篇
- Beego development blog system learning (II)
- R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram, and_ Set show in the point parameter_ The legend parameter is false, and the legend information is not displayed
- Global Capital Market 101:国内高净值人群最好的投资标的之一BREIT
- R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram and use scale_ color_ viridis_ D function specifies the color scheme of data points
- 各厂家rtsp地址格式如下:
猜你喜欢

Another miserable day by kotlin grammar

Our company has used this set of general solutions for 7 years, and has opened up dozens of systems, a stable batch!

Four Misunderstandings of Internet Marketing

Use of redis in projects

Map集合

3D视觉检测在生产流水的应用有哪些

MySQL 表的内连和外连

redis在项目中的使用

How difficult is data governance and data innovation?

MySQL索引和优化的理解学习
随机推荐
Map集合
Four Misunderstandings of Internet Marketing
TypeScript ReadonlyArray(只读数组类型) 详细介绍
Paper interpretation (AGC) attributed graph clustering via adaptive graph revolution
WebView, Scrollview sliding conflict correction
[cf] 803 div2 B. Rising Sand
Constructor, class member, destructor call order
beego开发博客系统学习(二)
Time function and clock_ Differences between gettime() functions
Ensemble de cartes
OpenMLDB Meetup No.4 会议纪要
来聊聊怎么做硬件兼容性检测,快速迁移到openEuler?
A high precision positioning approach for category support components with multiscale difference reading notes
R语言ggplot2可视化分面图(facet):gganimate包基于transition_time函数创建动态散点图动画(gif)、使用labs函数为动画图添加动态时间标题
A Generic Deep-Learning-Based Approach for Automated Surface Inspection-論文閱讀筆記
光谱共焦位移传感器的原理是什么?能应用那些领域?
Learn how to implement distributed locks in redis - my own understanding
R语言ggplot2可视化:使用ggplot2可视化散点图、aes函数中的size参数指定数据点的大小(point size)
Talk about how to do hardware compatibility testing and quickly migrate to openeuler?
1020. number of enclaves