[Set] the set framework

[Set] the set framework

1.hashset in jdk1.8 and above at the bottom do some modification, data will be sorted by default output rules regardless of whether the ordered using an iterator to take data into the data.
Here Insert Picture Description
2, will generate a hashCode value Hashset hash table to store data, the coordinate data corresponding hash table if hashCode values are equal, determines whether the equals true, hashCode if the values are different, then no calls equals, to automate the deduplication ;
Here Insert Picture Description

3.TreeSet has a Comparable interface so that the added element has its own collation, which is also known as natural sort ordering (but the elements can not be the object otherwise it will report java.lang.ClassCastException)
Here Insert Picture Description

4, when Treeset the objects given to the natural ordering rules you must follow these rules and to use the Comparable interface to determine the main conditions, and then determine the secondary condition, otherwise there will be data loss
Here Insert Picture Description

Comparator interface will use the rules and use the Comparable interface 5, in Treeset necessary to use natural ordering again using a different collation is the same
Here Insert Picture Description

6, TreeSet there is a special data structure to become a binary tree:
this data structure as a node has been added to the data, the data after the insertion node and the node is greater than the comparison row on the right, the left row of the node is less than, and so on , up from left to sort the data.

Guess you like

Origin blog.csdn.net/zyp_baoku/article/details/90441992
set
set
Recommended