当前位置:网站首页>Set in set (III)
Set in set (III)
2022-06-30 06:18:00 【Java shaping】
Catalog
Two 、set Self defined methods in
3、 ... and 、set: Storage out of order 、 Non repeatable data in HashSet For example, :
Four 、 The process of adding elements : With Hashset For example :( The underlying logic )
One 、 frame
/----Collection Interface : Single column set , Objects used to store one by one
/----Set Interface : Disordered storage , Non repeatable data ( Similar to the collection of high school )
/----HashSet : As set The main implementation class of the interface , Thread unsafe , Can be stored null value
/----LinkedHashSet: As HashSet Subclasses of , When traversing its internal data , You can traverse it in the order you add it
advantage : For frequent traversal operations ,LinkedHashSet Efficiency is higher than Hashset
/----TreeSet: You can add the specified properties of the object according to , Sort .Two 、set Self defined methods in
1. Set There are no additional new methods defined in the interface , All of them are collection The method declared in .
2. requirement : towards Set Data added in , Its class must be overridden hashcode( ) and equals()
requirement : Rewrite the hashCode() and equals() Be as consistent as possible : Equal objects must have equal hash codes , Generally, it is good to adjust it directly 3、 ... and 、set: Storage out of order 、 Non repeatable data in HashSet For example, :
1. Disorder : It's not equal to randomness . The data stored in the underlying array is not added in the order of the array index , It is sorted according to the hash value of the data . 2. Non repeatability : Make sure that the added elements follow equals() When judging , Can't return true. namely : Only one can be added to the same element .
Four 、 The process of adding elements : With Hashset For example :( The underlying logic )
We ask HashSet Add elements to it a, First call the element a Of the class hashCode() Method , Calculated element a Hash value of ,
This hash value is then calculated by some algorithm in HashSet The storage location in the underlying array ( That is to say : Index position ), Determine whether there are elements in this position of the array :
If there are no other elements in this position , The element α Add success .---> situation 1
If there are other elements in this position b( Or multiple elements in the form of a linked list ), Compare elements a And element b Of hash value :
If hash The value is different , The element α Add success .---> situation 2
If hash Same value , In turn, you need to call the element α Of the class equals() Method :
equals() return true, Elements α Add failure
equals() return faLse, The element α Add success .---> situation 3
For successful additions 2 And circumstances 3 for : Elements α And the existing data at the specified index position are stored in the form of linked list .
jdk 7 ∶ Elements a Put it in the array , Point to the original element .
jdk 8 ∶ The original element is in the array , Point to elements d
HashSet Bottom : Array + The structure of the list 5、 ... and 、LinkedHashSet Use
LinkedHashSet As HashSet Subclasses of , While adding data , Each data also maintains two indexes , Record the previous data and the next data of this data advantage : For frequent traversal operations ,LinkedHashSet Efficiency is higher than Hashset
6、 ... and 、TreeSet Use
1. towards Treeset Data added in , The requirement is Objects of the same class . 2. Two ways of sorting : Natural ordering ( Realization Comparable Interface ) and Custom sort ( Realization Comparator) 3. In natural order , The criterion for comparing whether two objects are the same is : compareTo() Return value , If it is 0, Then judge that the two objects are the same . without equals(). 4. Custom sorting , The criterion for comparing whether two objects are the same is : compare() Return value , If it is 0, Then judge that the two objects are the same . without equals().
Customized sorting code display :
@Test
public void test2() {
Comparator comparator = new Comparator() {
@Override
// Sort by age
public int compare(Object o, Object t1) {
if (o instanceof Person && t1 instanceof Person) {
Person p1 = (Person) o;
Person p2 = (Person) t1;
return Integer.compare(p1.getAge(), p2.getAge());
} else {
throw new RuntimeException(" Data type mismatch ");
}
}
};
TreeSet treeSet = new TreeSet(comparator);// Here is the constructor with parameters
treeSet.add(new Person(12, "q"));
treeSet.add(new Person(5, "a"));
treeSet.add(new Person(88, "l"));
treeSet.add(new Person(43, "b"));
treeSet.add(new Person(2, "d"));
treeSet.add(new Person(9, "d"));
Iterator iterator = treeSet.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}边栏推荐
- 583. 两个字符串的删除操作-动态规划
- 反编译正常回编译出现问题自己解决办法
- 583. deleting two strings - Dynamic Planning
- [exercise] basic practice letter graph of Blue Bridge Cup
- Unable to access the Internet at win10 /11 hotspot
- A complete performance test process
- CompletionService使用及原理(源码分析)
- 880. 索引处的解码字符串
- Common mistakes daily practice 01
- As function memo
猜你喜欢

Idea add database

Who doesn't want a blog site of their own - build a blog site WordPress

图片。。。。。

IP TCP UDP network encryption suite format
![[GPU] basic operation](/img/76/6b22368e3addd30aef1dd2258ee49a.jpg)
[GPU] basic operation

C language final experiment report (student achievement management system) source code

多线程进阶篇

Balanced binary tree judgment of Li Kou 110 -- classic problems
![[deep learning] data segmentation](/img/16/798881bbee66faa2fb8d9396155010.jpg)
[deep learning] data segmentation

重构之美:当多线程批处理任务挑起大梁 - 万能脚手架
随机推荐
【数据库】事务
RSA and AES
Problems related to pinduoduo store search, why can't new products be found on the shelves
ES6 deconstruction assignment
[wechat applet: single or multiple styles, background color, rounded corners]
requests. The difference between session () sending requests and using requests to send requests directly
[regular expression series] greedy and non greedy patterns
MySQL存储系统
Golang's handwritten Web Framework
观察者模式、状态模式在实际工作中的使用
Ultra simple STM32 RTC alarm clock configuration
PHP knowledge points
一个完整的性能测试流程
My experience in functional testing for so many years
Master slave synchronization of MySQL database to realize read-write separation
Unclear about glide loading picture
Data read / write: realize data read / write function based on C # script in unity
Usage of case, casez and casex statements in Verilog
Basic use of markdown
PC viewing WiFi password