MAP interface and implementation in Java

xiaoxiao2021-03-06  19

MAP interface and implementation: 1. The MAP interface MAP is a mapping from the key to the value. The key is not allowed to repeat, each key can map a value. Public interface map {// query operations

Int size (); boolean iSempty (); Boolean Containskey; Boolean ContainSValue; V Get (Object Key);

// MODIFICATION Operations V PUT (K Key, V Value); V Remove (Object Key);

// Bulk Operations

Void Putall (Map T); void clear ();

// views set keyset (); Collection value (); set > entryset ();

Interface entry {k getKey (); v getValue (); v setValue (v value); Boolean Equals (Object O); int hashcode ();}

// Compaison and hashing boolean equals (Object O); int hashcode ();}

The Entry interface and the implementation of MAP are mappings of key to values, and the Java Collection Framework is implemented with Map.Entry to encapsulate each key value, so that the elements in the map become map.entry, this It seems to indicate that MAP and SET have some type of communication. In fact, in the Hashset implementation, in the implementation of the Hashset, the elements of the HashSet are stored in the bottom of which is carried out by means of HashMap (using LinkedHashMap in some cases). // SIMPLEENTRY STATIC Class SimpleEntry Implements Entry {k kew; v value;

Public SimpleEntry (K Key, V Value) {this.key = key; this.value = value;

Public SimpleEntry (Entry e) {this.key = E.getKey (); this.Value = E.GetValue ();

PUBLIC K getKey () {return key;}

Public v getValue () {return value;}

Public v setValue (v value) {v oldvalue = this.value; this.value = value; returna}

Public Boolean Equals (Object O) {if (! (o instanceof map.entry) Return False; map.entry E = (map.entry) O; Return EQ (Key, E.GetKey () && EQ (Value, E.GetValue ());

Public int.com ((key == null)? 0: key.hashcode ()) ^ ((value == null)? 0: value.hashcode ());} public string toString () {Return Key "=" value;}

Private Static Boolean EQ (Object O1, Object O2) {RETURN (O1 == NULL? O2 == NULL: O1.Equals (O2)); A general implementation: HashMap and Treemap. Hashmap is implemented using a hash table, is the best comprehensive implementation of the MAP interface .TreeMap implements the MAP sub-interface SortedMap, which uses a red black tree as the underlying storage structure, providing the order of order Map storage. 2.1Hashmap and Hashtable comparison: Hashmap HashTable keys and values ​​allow empty allowable to allow multiple threads to synchronize NOTIONS, HashMap can easily get a synchronization view of HashMap by Map Synmap = Collections.SynchronizedMap (New Hashmap (...); HashTable has been re-created, this is to keep backward compatibility, ensuring that the old version of the program can continue to normally allow; it is also convenient for HashTable and new set frames (new HashTable implementation MAP) Interface). It is precisely because Hashtable has been modified, but it makes it mixed with new and old features, too complicated, bloated. The new MAP interface uses the enumeration function than the original HashTable, more powerful Iterator traversal tool. Meuanumeration is now Recommended use, not only because the Enumeration does not provide a traversal key or a value of security deletion of the element in the HashTable; and the name of Enumeration does not comply with the Sun's Java naming specification (using Enumeration more appropriate). Hashtable is in the sake of compatibility When implementing, the Enumeration internal class of Enumeration and Iterator interface has been implemented at the same time. So I recommend everyone with HashMap. 2.2 TreeMap TreeMap is a balanced binary tree, that is, each internal node has a leading node or said or Parent nodes and two child nodes. The member of the storage subsequent node is named Left and Right. Binary tree is balanced When the height of the leaf node to the root node work is most effective in a certain range. The reason for balancing a binary tree is to shorten from root nodes (this is the only node that can be directly referenced in the TreeMap class. The distance from each leaf node. The shorter distance, the less time required for accessing that node. The definition of balance tree is different from the trees in the Java2 collection frame. The red black tree is a kind of red black tree. Balanced tree, this balance tree definition is that there is no leaf node with a depth of the other leaf nodes more than two steps. This name is because each node of the tree is used to be red and black, the colors of the node are used to detect The balance of the tree. In the operation of the node insertion and delete, it may be rotated to maintain the balance of the tree. Generally and the worst case insertion, delete, the lookup time is O (LGN). TreeMap PUT method: public V PUT (K Key, V Value) {Entry t = root;

IF (t == null) {incrementSize (); root = new entry (key, value, null); return null;} while (true) {int CMP = Compare (key, t.key); IF (CMP == 0) {RETURN T.SETVALUE (Value);} else if (cmp <0) {if (t.left! = null) {t = t.left;} else {incrementSize (); t. Left = new entry (key, value, t); fixafterInsertion; return null;}} else {// cmp> 0 if (t.right! = null) {t = t. Right;} else {incrementsize (); t.right = new entry (key, value, t); fixafterInsertion (T.right); return null;}}}} 2.3 MAP implementation is not required When stored in an orderly, efficient HashMap can be used; if you need order storage, TreeMap can be used, but because TreeMap is stored in red black, it is required to be compared to H Ashmap more space and time overhead.

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

New Post(0)