Collectes framework in Java 2 - Map

xiaoxiao2021-03-06  55

In the Collections framework of Java 2, there are mainly two interfaces and their extensions and implementation classes: Collection interface and MAP interface. The difference between the two is that the former stores a set of objects, and the latter stores some keyword / value pairs. public interface java.util.Map {// Altering Methods public Object put (Object key, Object value); public Object remove (Object key); public void putAll (java.util.Map); public void clear (); // Querying Methods public Object get (Object key); public int size (); public boolean isEmpty (); public boolean containsKey (Object); public boolean containsValue (Object); public boolean equals (Object); // Viewing Methods public java. Util.set keyset (); // gets keys public java.util.collection value (); // Gets Values ​​public java.util.set entryset (); // gets mappings public static interface java.util.map.Entry { // a Map-entry (Single Key / Value Pair) public object getKey (); // Returns Current Entry key public object getValue (); // Returns Current Entry Value Public Object SetValue; Public Boolean Equals (Object); Public INT HashCode ();}} The MAP interface provides easy-to-use methods, through these methods, can query, view, modify the current map Content. Note that the KeySet () method of the MAP interface returns a set, and the set is an extension of the Collection interface, which contains a group of objects that are not repeated. Because the key in the MAP is not repetitive, you get a set object if all Key's KeySet () method is returned. The MAP interface itself also contains a map.Entry interface, and a map.Entry is a keyword / value pair in the map. The Entryset () method in the MAP interface returns a collection object, each of which implements the map.Entry interface. The GET (Object Key, Object Value) of the MAP interface has the same problem with the Remove (Object Key) method. Their return type is Object. When returns null, you can guess that the key does not exist before that method.

But only when NULL does not allow values ​​as MAP, you can speculate. All general implementations of all MAP interfaces allow NULL as Key or Value, which means that many things can be meant when returns a NULL value. Just because the general implementation allows the null value, you can't get the conclusion that the NULL value is mapped. If you know that there is no null value, the return null value means that there is no key in the mapping before calling that method. Otherwise, you must call ContainsKey (Object Key) to see if the key exists. Hashtable

Java.util.hashtable implements the MAP interface, using the HashCode () of the KEY object in HashTable as the relative storage address of the corresponding object, in order to implement the functionality of the object based on the keyword. So there is only one object that implements the HashCode () and equals () methods can be used as the key of HashTable. NULL values ​​cannot be used as a keyword or value. public class java.util.Hashtable extends Dictionary implements Cloneable, Map, Serializable {// Hashtable constructors // construct a default Hashtable with default capacity and load of 0.75 public Hashtable (); // construct a Hashtable with passed capacity and default load of 0.75 public Hashtable (int initialCapacity); // construct Hashtable with passed capacity and load public Hashtable (int initialCapacity, float load); // construct Hashtable with passed mapping public Hashtable (Map); // Hashtable specific methods // checks if Object is in Hashtable public boolean contains (Object); // returns Enumeration of elements in Hashtable public Enumeration elements (); // returns Enumeration of keys in hashtable public Enumeration keys (); // creates shallow copy of Hashtable (structure copied, but NOT Key / VALUES) Public Object Clone (); // prints out key / value pairs of Hashtable elements public String toString (); // reorganizes all elements in Hashtable, and increases Hashtable capacity protected void rehash (); // get Value from passed in key public Object get (Object); // INSERT Key / Value Pair Public Object Put (Object Key, Object Value);} Hashtable is an old tool class before the Java 2 Collection Framework, which has been replaced by HashMap under the new Java 2 episode frame. The difference between HashTable and Hashmap is mainly synchronized, the latter is a fast failure mechanism to ensure that the multi-threaded concurrency error will not occur (Fast-fail).

When initializing a HashTable, you can specify two parameters: initial capacity, load, which strongly affects the performance of HashTable. Capacity refers to the number of objects, and the load is the ratio of the number and capacity of the actual stored object in the list. If the initial capacity is too small, then HashTable needs constant expansion and rehash (), which is time consuming; if the initial capacity is too large, it will cause waste. Contrary to the load, the load is too small to cause space waste, and the load is too large and will time consuming (because this will cause more keywords to repeat, "HashTable uses a link table to store these repeating disappears) . The default value of the capacity is 11. The default value of the load is 0.75. In general, you can use the default value to generate a HashTable. In addition, most of the methods in HashTable are synchronized. Hashmap

HashMap basically implements all MAP interfaces. The signature of the method, everyone looks at the MAP interface. It mainly tells the methods in several MAP interfaces. According to the implementation of the collection framework, the hash table is a single-strand table as an array of elements, and two or more inlets with the same index value are linked into a single-link table. The hash table declares as follows: private entry [] table; component type Entry is the implementation of the map.Entry interface, and map.Entry is declared in the MAP interface. Map.Entry below is a simplified implementation of the interface: private static class Entry implements Map.Entry {int hashCode; Object key; Object value; Entry next; Entry (int hashCode, Object key, Object value, Entry next) {This.hashCode = hashCode; This.key = key; this.value = value; This.next = next;} public Object getKey () {return key;} public Object getValue () {return value;} public Object setValue (Object value) {Object OldValue = this.Value; this.Value = value; returna}} SortedMap is the sub-interface of the MAP interface, and the standard implementation of SortedMap is TreeMap to implement a sorted MAP. I am no longer explained here. The Java Rules (Java Rules) in this site have an in-depth study of Java2's collection framework. This article is limited to space, only fur. Interested friends, please refer to the collection of the Java2 source code implementation code and API DOC.

转载请注明原文地址:https://www.9cbs.com/read-115167.html

New Post(0)