The difference between HashSet and TreeSet
I. Issues
1. How does Hashset,treeset use the hashcode () and Equals () methods?
2. Why do the objects in Treemap,treeset implement the comparable interface?
Two. Answer:
1. HashSet is implemented through the HASHMAP, TreeSet is implemented through TreeMap, only set with only map key
2. Map key and set have a common feature is the uniqueness of the collection. TreeMap is a more orderly.
3.hashCode and Equal () are hashmap, because there is no need to sort, so just focus on positioning and uniqueness.
A. Hashcode is used to compute the hash value, and the hash value is used to determine the hash table index.
An index in the B.hash table holds a list of linked lists, so it is also possible to equal each object on the chain by using the method loop to actually locate the corresponding entry of the key value.
C. When put, if not located in the hash table, add a entry in front of the list, and if it is positioned, replace the value in entry and return the old value
D. When writing key hashcode () and equal (), be sure to note that they are not associated with a mutable attribute, otherwise the hashcode will change after the property has changed, and the equal will be false so that it is not found in the map. And such an object cannot be released because it cannot be found, thus becoming an invalid reference (the equivalent of a memory leak).
4. Because the treemap needs to be sorted, a comparator is required for the key value for the size comparison. Of course, it is also comparator positioning.
A. Comparator can be specified when creating TreeMap, which is used when sorting comparator.compare
B. If comparator is not specified at creation time, the Key.compareto () method is used, which requires the key to implement the comparable interface.
C. TreeMap is implemented using the tree data structure, so you can use the Compare interface to complete the positioning.
1. HashSet is implemented through the HASHMAP, TreeSet is implemented through TreeMap, only set with only map key
2. Map key and set have a common feature is the uniqueness of the collection. TreeMap is more of a sort of function.
3. Hashcode and Equal () are hashmap, because there is no need to sort, so just focus on positioning and uniqueness.
A. Hashcode is used to compute the hash value, and the hash value is used to determine the hash table index.
B. A list in the hash table is stored in a list, so also through the equal method to cycle the comparison of each object on the chain, you can really locate the key value corresponding to the entry.
C. When put, if not located in the hash table, add a entry in front of the list, and if it is positioned, replace the value in entry and return the old value
4. Because the treemap needs to be sorted, a comparator is required for the key value for the size comparison. Of course, it is also comparator positioning.
A. Comparator can be specified when creating TreeMap
B. If the creation is not determined, then the Key.compareto () method is used, which requires the key to implement the comparable interface.
C. TreeMap is implemented using the tree data structure, so you can use the Compare interface to complete the positioning.
Import Java.util.HashSet;
Import Java.util.Iterator;
public class Wpsklhashset
{
Use of Set in Java (no duplicate objects are allowed):
public static void Main (string[] args)
{
HashSet hashset=new hashset ();
String A=new string ("a");
String B=new string ("B");
String C=new string ("B");
Hashset.add (a);
Hashset.add (b);
System.out.println (Hashset.size ());
String Cz=hashset.add (c)? " This object does not exist ":" already exists;
SYSTEM.OUT.PRINTLN ("Test whether you can add Objects" +cz);
System.out.println (Hashset.isempty ());
Test to see if an object is already included
System.out.println (Hashset.contains ("A"));
Iterator Ir=hashset.iterator ();
while (Ir.hasnext ())