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 between 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 the 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 retrieve a number, such as 2, use the corresponding key:
Integer n = (Integer)numbers.get(“two”);
System.out.println(“two = ” + n);
Since the object used as a key will determine the position of the corresponding value by calculating its hash function, any object used as a key must implement the hashCode and equals methods. 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: override the equals method and hashCode method at the same time. Don't write just 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 that 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 duplicate elements, that is, any two elements e1 and e2 have e1.equals(e2)=false, and Set has at most one null element.
Set's constructor has a constraint that the passed-in Collection parameter cannot contain duplicate elements.
Please note: Mutable Objects 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 if sequential iteration is important to you, use a TreeSet. Otherwise, use a 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 HashSets is that the 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. You can use the int constructor to specify the initial capacity. The initial capacity of the HashSet to be allocated is 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 to 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 tuning parameters. In addition to clone, HashSet and TreeSet have only those operations required by their respective interfaces (Set and TreeSet), and no other operations.