Home  >  Article  >  Java  >  The difference between HashMap, Hashtable and HashSet in Java

The difference between HashMap, Hashtable and HashSet in Java

高洛峰
高洛峰Original
2017-01-19 10:26:531729browse

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

To add data, use put(key,value), and to remove data, use get(key). The time cost of these two basic operations is constant.

Hashtable adjusts performance through two parameters: initial capacity and load factor. Usually the default load factor 0.75 achieves a better balance of time and space. Increasing the load factor can save space but the corresponding search time will increase, which will affect operations like get and put.

A simple example of using Hashtable is as follows. Put 1, 2, and 3 into Hashtable, and their keys are "one", "two", and "three" respectively:
Hashtable numbers = new Hashtable( );
numbers.put(“one”, new Integer(1));
numbers.put(“two”, new Integer(2));
numbers.put(“three”, new Integer(3));

To take out a number, such as 2, use the corresponding key:
Integer n = (Integer)numbers.get(“two”);
System.out. println(“two = ” + n);

Since the object as the key will determine the position of the corresponding value by calculating its hash function, any object as the key must implement hashCode and equals method. The hashCode and equals methods inherit from the root class Object. If you use a custom class as a key, be very careful. According to the definition of the hash function, if the two objects are the same, that is, obj1.equals(obj2)=true, then Their hashCode must be the same, but if two objects are different, their hashCode is not necessarily different. If the hashCode of two different objects is the same, this phenomenon is called a conflict. The conflict will cause the time overhead of operating the hash table to increase. Therefore, try to define a well-defined hashCode() method to speed up hash table operations.

If the same object has different hashCode, the operation of the hash table will have unexpected results (the expected get method returns null). To avoid this problem, you only need to remember one thing: copy at the same time equals method and hashCode method, rather than just writing one of them. Hashtable is synchronous.​

HashMap Class ​
HashMap is similar to Hashtable, except that HashMap is asynchronous and allows null, that is, null value and null key. , but when treating HashMap as a Collection (the values() method can return a Collection), the time overhead of its iteration sub-operations is proportional to the capacity of the HashMap. Therefore, if the performance of iterative operations is very important, do not set the initial capacity of HashMap too high or the load factor too low.

WeakHashMap Class
WeakHashMap is an improved HashMap, which implements "weak references" to keys. If a key is no longer referenced externally, the key can be recycled by GC.

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

The constructor of Set has a constraint that the passed-in Collection parameter cannot contain duplicate elements.
Please note: Mutable objects (Mutable Object) must be handled with care. If a mutable element in a Set changes its state causing Object.equals(Object)=true, it will cause some problems.

Two common Set implementations are HashSet and TreeSet. Deciding which one to use is pretty straightforward. HashSet is much faster (constant time vs. log time for most operations), but does not provide ordering guarantees. If you need to use the operations in a SortedSet, or iterating sequentially is important to you, then use a TreeSet. Otherwise, use HashSet. It's a fair gamble for you not to use a HashSet most of the time.​​

One thing you should keep in mind about HashSet is that iteration is linear in terms of the sum of the number of entries and the capacity. Therefore, if iteration performance is important, an appropriate initial capacity should be chosen carefully. Choosing a capacity that is too large wastes both space and time. The default initial capacity is 101, which is generally more than you need. The initial capacity can be specified using the int constructor. To allocate a HashSet with an initial capacity of 17:

Set s= new HashSet(17);

HashSets also have a "tuning parameter" called the load factor. " . If you are very concerned about the space usage of your HashSet, read the HashSet text for details. Otherwise, just use the default value. If you accept the default load factor, but you do want to specify an initial capacity, pick a number that is approximately twice the capacity you expect your Set will grow to. If your guess is off base, it can grow, or just waste a little space. But there are no big problems. If you know the best value for the correct size, use it; if you don't know, use an old value, or use an even value. It's really not very important. These things only make HashSet slightly better.​​

TreeSet has no adjustment parameters. In addition to clone, HashSet and TreeSet have only those operations required by their respective interfaces (Set and TreeSet), and no other operations.


For more articles related to the differences between HashMap, Hashtable and HashSet in Java, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn