当前位置:网站首页>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());
}
}
}边栏推荐
- Summation of basic exercise sequence of test questions
- ES6 arrow function
- Master slave synchronization of MySQL database to realize read-write separation
- Switch must be better than if Else fast
- Jgaurora A8 configuration file
- Go common judgments
- 【学习强化学习】总目录
- ES6 array
- Ten years' miscellaneous thoughts
- DOM (document object model) document XML file object model
猜你喜欢

Cisco vxlan configuration

SHELL

飞升:基于中文分词器IK-2种自定义热词分词器构建方式showcase & 排坑showtime

Completabilefuture: from understanding to mastering, here are all you want to know

图像处理7-图像增强

Dynamic routing job

从底层结构开始学习FPGA----RAM IP核及关键参数介绍

My experience in functional testing for so many years

Loading class `com. mysql. jdbc. Driver‘. This is deprecated. The new driver class is `com. mysql. cj. jdb

Unable to access the Internet at win10 /11 hotspot
随机推荐
CompletionService使用及原理(源码分析)
Problems related to pinduoduo store search, why can't new products be found on the shelves
旋转标注工具roLabelImg
MySQL storage system
Using C language pure for loop to implement ilovey
Balanced binary tree judgment of Li Kou 110 -- classic problems
uniapp 微信小程序返回上一页并刷新
IP TCP UDP network encryption suite format
C language final experiment report (student achievement management system) source code
Common NPM install errors
Notes of the first week of 2021 Chengdu Arts and Sciences cloud computing intensive training class
Detailed description of methods in the interface
Intelligent question - horse racing question
ES6 deconstruction assignment
Master slave synchronization of MySQL database to realize read-write separation
General contents of learning reinforcement learning
46. full arrangement -dfs double hundred code
图片。。。。。
There is a group of students' score {99, 85, 82, 63, 60}. To add a student's score, insert it into the score sequence and keep the descending order
Unable to access the Internet at win10 /11 hotspot