The difference between HashMap and Hashtable and HashSet in Java

  • 2020-04-01 02:17:55
  • OfStack

Hashtable class    
Hashtable inherits the Map interface and implements a Hashtable for key-value mapping. Any non-null object can be used as a key or value.    

Put (key,value) is used to add data, and get(key) is used to fetch data. The time cost of these two basic operations is constant.    

The Hashtable initial    Capacity and load    Factor adjusts performance with two parameters. Usually default load    Factor    The balance of time and space is well achieved. Increasing load    Factor can save space but will take longer to find, which can affect operations like get and put.    

A simple example of using a Hashtable is to place 1, 2, and 3 into a Hashtable with the keys "one", "two", "three" :    
Hashtable    Numbers    =     New    Hashtable ();    
Numbers. The put (" one ",     New    Integer (1));    
Numbers. The put (" two ",     New    Integer (2));    
Numbers. The put (" three ",     New    Integer (3));    

To pick a number, such as 2, use the key: & PI;  
Integer    N    =     (Integer) Numbers. Get (" two ");    
System. Out.println (" two    =     "    +     N);    

Since the object as key will determine the location of its corresponding value by calculating its hash function, any object as key must implement the hashCode and equals methods. The equals and hashCode methods inherited from the root class Object, if you use a custom class as the key, be very careful, as defined by the hash function, if two objects are the same, namely obj1) equals (obj2) = true, they must be the same hashCode, but if two objects are different, the hashCode they may not different, if two different objects of the same hashCode, this phenomenon is called conflict, conflict can lead to hash operation time cost increase, So the hashCode() method, as defined as possible, will speed up the operation of the hash table.    

If the same object has a different hashCode, the operation on the hash table can yield unexpected results (the expected get method returns null). To avoid this problem, just remember to copy both equals and hashCode methods, not just one.     Hashtable is synchronous.    

A HashMap class    
A HashMap is similar to a Hashtable, except that a HashMap is asynchronous and allows null, null&cake;   The value and null    The key. , but the (values() method returns a Collection when a HashMap is treated as a Collection, and its iterator operation time is proportional to the capacity of the HashMap. So, if the performance of the iteration operation is important, don't set the initialization capacity of the HashMap too high, or the load ;   Factor is too low.    

WeakHashMap class    
WeakHashMap is an improved HashMap that implements a "weak reference" to a key. If a key is no longer externally referenced, it can be recovered by the GC.

HashSet refer to the description of Set    
Set is a Collection that does not contain duplicate elements, that is, any two elements e1 and e2 have e1.equals(e2)=false, and Set has at most one null element.    

The constructor of Set has a constraint that the incoming Collection parameter cannot contain duplicate elements.
Please note: Mutable objects must be handled with care.   Object). This can cause problems if a mutable element in a Set changes its state and causes object.equals (Object)=true.

The two common Set implementations are HashSet and TreeSet. To decide which one to use, that's pretty straightforward. HashSet is much faster (for most operations it is constant time versus logarithmic time (constant ;   Time    Vs.     Log    Time),     But no sort guarantee is provided. If you need to use     SortedSet    Is important to you, or iterating in order, then use     TreeSet.     Otherwise, use & cake;   HashSet.     Not used most of the time     HashSet    It's a fair gamble for you.        

One thing to keep in mind about HashSet is that the iteration is linear in terms of the sum of entries and capacities. Therefore, if iterative performance is important, it is prudent to choose an appropriate initial capacity. Choosing too much capacity wastes both space and time.     The default initial capacity is 101,    Generally speaking, it is more than you need. You can use & cake;   Int    Constructor to specify the initial capacity. To allocate     HashSet    Is the initial capacity of 17:       

Set  S =   New  HashSet (17);        

Another hashset is called     Load factor (load    Factor) "tuning parameters (tuning    The parameter) "    . If you are concerned about the use of space in your HashSet, read the HashSet text for details. Otherwise, use the default values. If you accept the default load factor, but you do want to specify the initial capacity, then choose one that is about what you expect your & PI to be.   Set    Will grow to twice the capacity. If your guess is off the mark, it can grow, or just waste a little space. But nothing serious. If you know an optimal value for the right size, use it; If not, use an old value, or an even value. It's not really that important. These things can only be     HashSet    A little bit better.        

TreeSet did not adjust the parameters. With the exception of clone, both HashSet and TreeSet have only those operations required by their respective interfaces (Set and TreeSet) and nothing else.


Related articles: