[Java] Collection (2) Set

1.Basic introduction to Set interface

  • Out of order: The access sequence is inconsistent
  • No duplication: Duplication can be removed
  • No index: There is no indexed method, so you cannot use an ordinary for loop to traverse, nor can you obtain elements through indexes

2.Set collection implementation class

  • HashSet: Unordered, non-repeating, no index
  • LinkedHashSet: ordered, non-duplicate, no index
  • TreeSet: sortable, non-repeating, no index


3. Common methods in Set interface

1) add: add a single element

2)remove: delete the specified element

3)contains: Find whether the element exists

4) size: Get the number of elements

5) isEmpty: determine whether it is empty

6) clear: clear

7) addAll adds multiple elements

8) containsAll: Find whether multiple elements exist

9) removeAll: delete multiple elements

4.HashSet

Comprehensive description of HashSet

1) The implementation of HashSet depends on HashMap, and the values ​​of HashSet are stored in HashMap.

2) Null values ​​can be stored, but there can only be one

3) There is no guarantee that the elements are in order. The result of the index is determined after hashing.

4) Cannot duplicate objects

The underlying principle of HsahSet

1) The bottom layer of HashSet isarray + linked list + red-black tree

2) When adding an element, first get the hash value - it will be converted to -> index value (int index=(array length-1)&hash value)

3) Find the storage data table table and see if there are elements stored at this index position.

4) If not, join directly

5) If there is , call equals to compare. If they are the same, give up adding. If they are not the same, add them to the end.

6) In Java8, if the number of elements in a linked list reaches TREEIFY THRESHOLD (the default is 8), and the size of the table >=MIN TREEIFY CAPACITY (the default is 64), It will be treed (red-black tree)

HashSet expansion mechanism

1) The bottom layer of HashSet is HashMap. When it is added for the first time, the table array is expanded to 16, and the threshold is 16*The load factor (loadFactor) is 0.75 = 12

2) If the table array reaches the critical value 12, it will be expanded to 16 * 2 = 32, and the new critical value is 32 * 0.75 = 24, and so on.

3) In Java8, if the number of elements in a linked list reachesTREEIFY THRESHOLD (the default is 8) and the size of the table> =MIN TREEIFY CAPACITY (default 64) will perform tree formation (red-black tree)< a i=6>, otherwise the array expansion mechanism will still be used

5.LinkedHashSet

Comprehensive description of LinkedHashSet

1) LinkedHashSet is a subclass of HashSet

2) The bottom layer of LinkedHashSet is a LinkedHashMap, which maintains an array + doubly linked list.

3) LinkedHashSet determines the storage location of the element based on the hashCode value of the element, and uses a linked list to maintain the order of the elements, which makes the elements appear to be saved in insertion order.

4) LinkedHashSet does not allow duplicate elements

The underlying mechanism of LinkedHashSet 

1) A hash table and doubly linked list are maintained in LinkedHastSet (LinkedHashSet has head and tail)

2) Each node has pre and next attributes, which can form a doubly linked list

3) When adding an element, first find the hash value, then find the index, determine the position of the element in the hashtable, and then add the added element to the doubly linked list (if it already exists, do not add it [the principle is the same as hashset])

tail.next = newElement // Simple specification
newElement.pre = tail

tail = newEelment;
4) In this case, we can also ensure that the insertion order and traversal order are consistent when traversing LinkedHashSet

6.TreeSet

Characteristics of TreeSet collection

  • Sortable, non-repeating, no index
  • The bottom layer is based on red-black tree to implement sorting, and the performance of addition, deletion, modification and query is good.

Customized sorting rules for the TreeSet collection
When the TreeSet collection stores objects of a custom type, the sorting rules must be specified. The following two methods are supported to specify the comparison rules.
Method 1
Let the custom class (such as student class) implement the Comparable interface , rewrite the compareTo method inside to specify the comparison rules.
Method 2
By calling the parameter constructor of the TreeSet collection, you can set the Comparator object (comparator object, used to specify comparison rules)
public TreeSet(Comparator<? super E> comparator)
In the two methods, the sorting rules are:
Specify the element position according to the return value of the compareTo method Such as

If the return value is a negative number, it means that the currently stored element is a smaller value and is stored on the left

If the return value is 0, it means that the currently stored elements are equal and not stored.

If the return value is a positive number, it means that the currently stored element is a larger value and is stored on the right side

Note: If the class itself implements the Comparable interface, the TreeSet collection also comes with it Comparator, by default, the comparator that comes with the collection is used for sorting

7. Tell me about the similarities and differences between HashSet, LinkedHashSet and TreeSet?

HashSet, LinkedHashSet and TreeSet are all implementation classes of the Set interface, all of which ensure unique elements and are not thread-safe. Their differences:

  • The main difference between HashSet, LinkedHashSet and TreeSet is the underlying data structure:
  1. The underlying data structure of HashSet is a hash table (implemented based on HashMap)
  2. The underlying data structure of LinkedHashSet is a linked list and a hash table. The insertion and removal order of elements satisfies FIFO.
  3. The underlying data structure of TreeSet is a red-black tree. The elements are ordered. The sorting methods include natural sorting and customized sorting.
  • The different underlying data structures lead to different application scenarios of the three:
  1. HashSet is used in scenarios where the order of insertion and removal of elements does not need to be guaranteed.
  2. LinkedHashSet is used to ensure that the insertion and removal order of elements meets FIFO scenarios
  3. TreeSet is used to support custom sorting rules for elements.
     

Guess you like

Origin blog.csdn.net/weixin_63357306/article/details/134374614