当前位置:网站首页>Set interface and set sub implementation classes
Set interface and set sub implementation classes
2022-06-23 08:18:00 【Liang FuFu】
1.Set Interface
1.Set Interface features : No repeating elements , Element uniqueness
Disorder
2.Set and List The same access method
2.HashSet aggregate
1. When calling HashSet Of add() Method to store elements , Will call first hashCode() Method , Get hash value , Calculate a storage location based on the hash value of the object , If the position has no elements , Then deposit ; If there are elements in that location , Call equals() Method to compare the currently stored element with the element at that location . return false, Store elements in a collection , return true, Then discard the element .
Custom types need to be overridden hashCode() and equals() Method
3.TreeSet aggregate
1. The underlying dependence TreeMap,TreeMap The underlying data structure is a red black tree structure
2. Use different construction methods , Will choose different sorting methods
Natural ordering :TreeSet(), Interface Comparable, rewrite compareTo Method
Comparator sort :TreeSet(Comparator<E> comparator)
Interface Comparator,compare Method
3.1 Natural sorting implementation code
public class Student implements Comparable<Student>{
private String name;
private int age;
// Here is the reference structure , No arguments structure ,get/set Method
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
// rewrite compareTo Method
public int compareTo(Student s){
int num = this.age - s.age ;// From small to large
int num2 = (num==0)?(this.name.compareTo(s.name)):num ;
return num2;
}
}
3.2 Comparator sorting implementation code
Mode one : Define sub implementation classes
public class MyComparator implements Comparator<Student> {
@Override
public int compare(Student s1, Student s2) {
int num = s1.getAge() - s2.getAge() ;
int num2 = (num==0)?(s1.getName().compareTo(s2.getName())):num ;
return num2;
}
}
TreeSet<Student> ts = new TreeSet<>(new MyComparator()) ;
Mode two : Anonymous inner class
TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num = s2.getAge() - s1.getAge() ;
int num2 = (num==0)?(s1.getName().compareTo(s2.getName())):num ;
return num2;
}
}) ;
Mode three : lambda expression
TreeSet<Student> ts = new TreeSet<>((s1,s2) ->{
int num = s2.getAge() - s1.getAge() ;
int num2 = (num==0)?(s1.getName().compareTo(s2.getName())):num ;
return num2;
}
) ;
4. Generic
<?>: arbitrarily java type
<? extends E>: Down limit ,E This type and its subclasses
<? super E>: Limit upward ,E This type and its parent class
5.ArrayList Set nesting
public class Test{
main(){
ArrayList<ArrayList<Student>> bigArray = new ArrayList<>();
ArrayList<Student> array1 = new ArrayList<>() ;
// Create a small collection
ArrayList<Student> array1 = new ArrayList<>() ;
ArrayList<Student> array1 = new ArrayList<>() ;
Student s1 = new Student(" Liu bei ",35) ;
Student s2 = new Student(" Cao Cao ",40) ;
array1.add(s1) ;
array1.add(s2) ;
// Add a small set to a large set
bigArray.add(array1) ;
// Second subset
ArrayList<Student> array2 = new ArrayList<>() ;
// Add two student data
Student s3 = new Student(" Tang's monk ",50) ;
Student s4 = new Student(" The Monkey King ",42) ;
array2.add(s3) ;
array2.add(s4) ;
// Add a small set to a large set
bigArray.add(array2) ;
// The third subset
ArrayList<Student> array3 = new ArrayList<>() ;
// Add two student data
Student s5 = new Student(" Song Jiang ",39) ;
Student s6 = new Student(" ximen qing ",28) ;
array3.add(s5) ;
array3.add(s6) ;
// Add a small set to a large set
bigArray.add(array3) ;
// Easy to traverse ----> enhance for
// The data type of the current large collection : ArrayList<ArrayList<Student>>
System.out.println(" The student information is as follows ");
for(ArrayList<Student> myArray:bigArray){
// subset ArrayList<Student> ---> The data type inside is Student
for(Student s :myArray){
System.out.println("\t"+s.getName()+"\t"+s.getAge());
}
}
边栏推荐
- MFC radio button grouping
- INT 104_LEC 06
- [paper notes] catching both gray and black swans: open set supervised analog detection*
- 图像分割-改进网络结构
- 给你的win10装一个wget
- Vulnhub | DC: 4 |【實戰】
- How can easycvr access the Dahua CVS video recorder and download a video file with an empty name?
- APM performance monitoring practice of jubasha app
- GTEST死亡测试
- Location of firewalld configuration file
猜你喜欢

数据资产为王,解析企业数字化转型与数据资产管理的关系

渲染效果图哪家好?2022最新实测(四)

Image segmentation - improved network structure

观察者模式

看了5本书,我总结出财富自由的这些理论

Socket programming (multi process)

为什么用生长型神经气体网络(GNG)?

如何在conda虚拟环境开启jupyter-notebook

VTK. Le bouton gauche de la souris JS glisse pour changer le niveau et la largeur de la fenêtre

After reading five books, I summarized these theories of wealth freedom
随机推荐
Sequence table Curriculum
Image segmentation - improved network structure
MFC radio button grouping
Observer mode
Vulnhub | DC: 4 | [combat]
Vulnhub | dc: 3 | [actual combat]
4-绘制椭圆、使用定时器
2 corrections de bogues dans l'outil aquatone
typeScript的介绍与变量定义的基本类型
5-旋转的小菊-旋转画布和定时器
jmeter压测结果分析
C Advanced Learning -- extended method (this)
Openvino series 19 Openvino and paddleocr for real-time video OCR processing
目标检测中的多尺度特征结合方式
Commonly used bypass methods for SQL injection -ctf
Vulnhub | dc: 4 | [actual combat]
Active Directory之AD对象
7-调色板-CALayer和触摸
aquatone工具 中的2個bug修複
Structure and usage of transform