Introduction to the Basic java Set (3), javaset
Today, we mainly talk about the summer set. We will take an hour every day to sum up and make life better!
-- < Java. Util> -- Set Interface:
Data Structure: data storage method;
The methods in the Set interface are the same as those in the Collection.
| -- HashSet:
The HashSet class directly implements the Set interface. Its underlying layer is actually packaged with a HashMap for implementation. HashSet uses the HashCode algorithm to access elements in a set. Therefore, it has good reading and searching performance.
The underlying data structure is a hash table, threadIs not synchronized.Unordered and efficient; HashSet is thread unsafe and the element value can be NULL
The HashSet set ensures the uniqueness of elements.: Completed through the hashCode method of the element and the equals method.
HashSet needs to judge whether two elements are equal through equals and HashCode at the same time. The specific rule is: if the two elements are true through equals, and the hashCode of the two elements is equal, the two elements are equal (repeated ).
When the hashCode value of an element is the same, the system checks whether the equals of the element is true.
If it is true, it is regarded as the same element and is not stored. If it is false, it is stored.
If the hashCode value is different, equals is not judged, thus improving the object comparison speed.
Remove HashCode
Disable parse se
| -- Javashashset:
The sub-class of the sorted hashset. (Never used)
| -- TreeSet:
Sorts the elements in the Set in the specified order.Not synchronized. The underlying data structure of TreeSet is a binary tree.
Principle of hash table:
1. Calculate the hash algorithm for the keywords (Special Data in the object) in the object element and obtain a specific algorithm value. This value is calledHash Value.
2. the hash value is the position of this element.
3. If the hash value conflicts, Judge again whether the object corresponding to this keyword is the same. If the objects are the same, they are not stored because the elements are repeated. If the object is different, it is stored, and the hash value of the original object is extended along with 1.
4. The structure for storing hash values is called a hash table.
5. Since the hash table is stored Based on the hash value, it is best to ensure that the key words of the object are unique to improve efficiency.
In this way, we can try to judge whether the objects corresponding to the keywords are the same, which improves the operation efficiency of the hash table.
For the ArrayList set, the equals method is used to determine whether the element exists or whether the underlying element is deleted.
For a HashSet set, determine whether an element exists or delete it. The underlying layer is based on the hashCode method and equals method.
TreeSet:
It is used to sort elements in the specified sequence in the Set. The sorting depends on the comparison of elements.
If the element is not comparable, A ClassCastException exception occurs during running.
Therefore, elements are required.Implement the Comparable Interface, Force the element to have a comparison,Rewrite compareTo Method.
Determine the position of an element in the TreeSet data structure based on the return value of the compareTo method.
The TreeSet method ensures the uniqueness of elements:Is to see whether the result of the comparison method is 0. If return 0 is returned, the two objects are duplicated and are not saved.
Note:During the comparison, if the judgment element is not unique, for example, the same name and age, it is regarded as the same person.
In Judgment, the primary and secondary conditions need to be divided. When the primary conditions are the same, the secondary conditions are judged and sorted by the secondary conditions.
There are two methods to sort a TreeSet set: Comparable and Comparator:
1: to make the elements have a comparison, the element object must implement the Comparable interface to overwrite the compareTo method.
2: to make the set have a comparison, You need to define a Comparator that implements the Comparator interface, overwrite the compare method, and pass the object as an actual parameter to the constructor of the TreeSet set.
The second method is more flexible.
Traverse Set
1. iterative traversal: Set <String> set = new HashSet <String> (); Iterator <String> it = set. iterator (); while (it. hasNext () {String str = it. next (); System. out. println (str);} 2. for Loop traversal: for (String str: set) {System. out. println (str );}