1. Problem
1. How does HashSet, TreeSet use hashCode() and equal() methods
2. When and why do objects in TreeMap and TreeSet implement Comparable interface?
2. Answer:
1. HashSet is implemented through HashMap, TreeSet is implemented through TreeMap, but Set only uses the Map key.
2. The key and Set of Map both have a common feature, which is the uniqueness of the set. TreeMap has an additional order.
3. hashCode and equal() are used for HashMap. Because there is no need to sort, you only need to pay attention to positioning and uniqueness.
a.hashCode is used to calculate hash value, and hash value is used to determine the hash table index.
b. An index in the hash table stores a linked list, so you have to loop through the equal method to compare each object on the chain before you can truly locate the Entry corresponding to the key value.
When c.put, if the hash table is not located, add an Entry before the linked list. If it is located, replace the value in the Entry and return the old value.
d. When overwriting the hashCode() and equal() of the key, be careful not to associate them with mutable properties. Otherwise, after the property changes, the hashCode will change and equal will also be false, so that it will not be found in the map. Moreover, such an object cannot be released because it cannot be found, which will become an invalid reference (equivalent to a memory leak).
4. Since TreeMap requires sorting, a Comparator is needed to compare the size of the key value. Of course, it is also positioned using Comparator.
a.Comparator can be specified when creating TreeMap, and then use Comparator.compare when sorting
b. If the Comparator is not specified during creation, the key.compareTo() method will be used, which requires the key to implement the Comparable interface.
c.TreeMap is implemented using Tree data structure, so positioning can be completed using the compare interface.
import java.util.HashSet;import java.util.Iterator;public class WpsklHashSet{//Use of Set in java (duplicate objects are not 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 an object can be added"+cz);System.out.println(hashSet.isEmpty());//Test whether an object is already contained in it System.out.println(hashSet.contains("A"));Iterator ir=hashSet.iterator(); while(ir.hasNext()){System.out.println(ir.next());}//Test whether an object can delete System.out.println(hashSet.remove("a"));System.out.println(hashSet.remove("A"));//After testing, if you want to use the ir variable again, you must re-update the following ir=hashSet.iterator(); while(ir.hasNext()){System.out.println(ir.next());}}}/*** Through this program, you can also test the disorder of the added elements of the tree set and the order of the output */import java.util.TreeSet;import java.util.Iterator;public class TreeSetTest{public static void main(String[] args){TreeSet tree = new TreeSet();tree.add("China");tree.add("America");tree.add("Japan");tree.add("Chinese");Iterator iter = tree.iterator(); while(iter.hasNext()){System.out.println(iter.next());}}}Also, I'm turning on some other differences (thanks to my andygulin friend who "baidu knows"):
1. TreeSet is implemented by a two-difference tree. The data in Treeset is automatically sorted and null values are not allowed.
2. HashSet is implemented by a hash table. The data in the HashSet is unordered. You can put null, but you can only put one null. The values in both cannot be repeated, just like the unique constraint in the database.
3. HashSet requires that the object placed must implement the HashCode() method. The object placed is marked with the hashcode code, and the hashcode object with the same content has the same hashcode, so the content placed cannot be repeated. But objects of the same class can be placed in different instances.
Summarize
The above is all the content of this article about the difference between the usage methods of HashSet and TreeSet, and I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!