Map Introduction
The object that maps the key to a value. A map cannot contain duplicate keys, and each key can be mapped to at most one value. This interface replaces the Dictionary class, which is completely an abstract class, not an interface.
The map interface provides three collection views that allow you to view the contents of a map in the form of a keyset, a set of values, or a key-value mapping relationship. The mapping order is defined as the order in which the iterator returns its elements on the mapped collection view. Some mapping implementations can explicitly guarantee their order, such as the TreeMap class, and other mapping implementations do not guarantee the order, such as the HashMap class.
Note: You must be cautious when using a mutable object as a mapping key. When an object is a key in a map, the mapping behavior is indeterminate if the value of the object is changed in a way that affects the equals comparison. A special case of this prohibition is that a mapping is not allowed to contain itself as a key. Although you allow a map to contain itself as a value, be careful: the definition of the Equals and Hashcode methods is no longer explicit on such a mapping.
Map Interface:
Map provides a mapping of key to value. A map cannot contain the same key, and each key can map only one value. The map interface provides a view of 3 collections, which can be treated as a set of key sets, a set of value sets, or a set of key-value mappings.
Hashtable class
Hashtable inherits the map interface and implements a hash table of key-value mappings. Any object that is Non-null (non-null) can be a key or value.
Adding data using put (key, value), fetching data using get (key), the time cost of these two basic operations is constant. Hashtable adjusts performance by initial capacity and load factor two parameters. Typically, the default load factor 0.75 achieves a better balance of time and space. Increasing the load factor saves space but the corresponding lookup time increases, which can affect operations like get and put.
Using Hashtable for example, put 1,2,3 into Hashtable, their key is "one", "two", "three":
Hashtable numbers = new Hashtable ();
Numbers.put ("One", New Integer (1));
Numbers.put ("Two", New Integer (2));
Numbers.put ("Three", New Integer (3));
To remove a number, such as 2, use the corresponding key:
Integer n = (integer) numbers.get ("two");
System.out.println ("two =" + N);
Because an object that is a key will determine the location of its corresponding value by calculating its hash function, any object that is a key must implement the Hashcode and Equals methods. The hashcode and Equals methods inherit from the root class object, and if you use a custom class as a key, be very careful, as defined by the hash function, if two objects are the same, that is, Obj1.equals (OBJ2) =true, Their hashcode must be the same, but if two objects are different, their hashcode is not necessarily different, and if the hashcode of two different objects is the same, this phenomenon is called conflict, and the conflict causes the time overhead of manipulating the hash table, so as to define the hashcode () method to speed up the operation of the hash table.
If the same object has a different hashcode, the operation of the hash table will have unexpected results (the expected GET method returns null), to avoid this problem, you need to keep in mind one: you have to copy both the Equals method and the Hashcode method, not just one.
The Hashtable is synchronized.
HashMap class
HashMap is similar to Hashtable, except that HashMap is unsynchronized and allows NULL, that is, null value and null key. , but when HashMap is treated as collection (the values () method returns collection), its iterative child operation time cost is proportional to the capacity of the HashMap. Therefore, if the performance of an iterative operation is significant, do not set the HASHMAP initialization capacity too high, or the load factor too low.
Weakhashmap class
Weakhashmap is an improved hashmap, which implements a "weak reference" to key, and if a key is no longer referenced externally, the key can be reclaimed by GC.
above is the introduction of the Java Map Interface, Learning Java programming students can refer to.