Analysis of the difference between HashSet and TreeSet

  • 2020-12-16 05:57:13
  • OfStack

Problem 1.

1. How do HashSet,TreeSet use hashCode() and equal()

2. When and why do objects in TreeMap implement the Comparable interface ?

2. Answer:

1.HashSet is implemented by HashMap,TreeSet by TreeMap, but Set only uses key of Map.

2. Both key and Set of Map have one property in common, that is, the uniqueness of sets.TreeMap has one more order.

3.hashCode and equal() are used by HashMap. Since there is no sorting, you only need to focus on positioning and uniqueness.

a.hashCode is used to calculate the hash value and the hash value is used to determine the index of the hash table.

Each index in the ES43en.ES44en table holds one linked list, so it is necessary to loop through the equal method to compare each object on the chain to actually locate the key value corresponding to Entry.

At ES49en. put, if it is not located in hash table, add one Entry before the linked list. If it is located, replace value in Entry and return the old value

d. Overwrite key hashCode () and equal (when) one must pay attention to, don't they associated with variable properties, otherwise hashCode will change after properties changed, equal false will be, so we won't be able to can not find it in the Map, and such objects because she couldn't find it so no release, thus becomes an invalid references (equivalent to a memory leak).

4. Since TreeMap needs to be sorted, 1 Comparator is needed for the key value size comparison. Comparator is also used for positioning, of course.

a. Comparator can be specified when creating TreeMap, where sorting uses Comparator. compare

b. If Comparator was not specified when it was created, the key.compareTo () method is used, which requires key to implement the Comparable interface.

c. TreeMap is implemented using the Tree data structure, so the positioning can be done using the compare interface.


import java.util.HashSet;
import java.util.Iterator;
public class WpsklHashSet
{
	//java  In the Set The use of ( 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 ":" existing ";
		System.out.println(" Tests whether an object can be added  "+cz);
		System.out.println(hashSet.isEmpty());
		// Tests 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());
		}
		// Tests whether an object can be deleted 
		System.out.println(hashSet.remove("a"));
		System.out.println(hashSet.remove("A"));
		// After the test , If you want to use it again ir variable , The following must be updated 
		ir=hashSet.iterator();
		while(ir.hasNext())
		{
			System.out.println(ir.next());
		}
	}
}
/**
*  With this program, you can also test the orderliness of the added elements of the tree set and the orderliness 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());
		}
	}
}

There are some other differences (thanks to andygulin friend "baidu know") :

1. TreeSet is implemented by 2-difference tree. The data in Treeset is automatically sorted, and the value of null is not allowed.

2. HashSet is implemented by hash table. The data in HashSet is unordered and can be put into null, but only one null.

3. HashSet requires that the object put in must implement the HashCode() method. The object put in is identified by hashcode code, while the String object with the same content, hashcode is one, so the content put in cannot be repeated. But objects of the same class can be put into different instances.

conclusion

That is the end of this article on the use of HashSet and TreeSet methods of difference analysis, I hope to help you. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: