/** * An object, the maps keys to values. A map cannot contain duplicate keys; * Each key can map to the most one value. * * <p>this interface takes the place of the <tt>Dictionary</tt> class, which * was a totally abstract C Lass rather than an interface. * * <p>the <tt>Map</tt> interface provides three <i>collection views</i>, which * allow a MA P ' s contents to be viewed as a set of keys, collection of values, * or set of key-value mappings. The <i>order</i> of a map is defined as * the order in which the iterators on the map ' s collection views retur n their * elements. Some map implementations, like the <tt>TreeMap</tt> class, make * specific guarantees as to their order; Others, like the <tt>HashMap</tt> * class, does not. * * <p>note:great care must is exercised if mutable objects is used as map * keys. The behavior of a map is not specified if the value of an object was * changed in a manner thaT affects <tt>equals</tt> comparisons while the * object was a key in the map. A special case of the prohibition is the it is not permissible for a maps to contain itself as a key. While it was * permissible for a map to contain itself as a value, extreme caution was * advised:the <tt>equals</t T> and <tt>hashCode</tt> methods is no longer * well defined on such a map. * * <p>all General-purpose map implementation classes should provide * "standard" constructors:a void (no Argum Ents) constructor which creates a * empty map, and a constructor with a single argument of type <tt>map</tt> * which creates a new map with the same key-value mappings as its argument. * In effect, the latter constructor allows the user-to-copy any map, * producing an equivalent map of the desired class. There is no-to-enforce this recommendation (as interfaces cannot contain constructors) but * all of the General-pur Pose map implementationsIn the JDK comply. * * <p>the "destructive" methods contained in this interface, that's, the * methods that modify the map on which th EY operate, is specified to throw * <tt>UnsupportedOperationException</tt> If this map does not support the * Operation. If This was the case, these methods could, but was not a required * to, throw an <TT>UNSUPPORTEDOPERATIONEXCEPTION</TT > If the invocation would * has no effect on the map. For example, invoking the { @link #putAll (MAP)} * method on a unmodifiable map may, but was not required to, throw the * exception if the map whose mapping S was to being "superimposed" is empty. * * <p>some Map implementations has restrictions on the keys and values they * may contain. For example, some implementations prohibit null keys and values, and some has restrictions on the types of their keys. Attempting * To insert a ineligible key or value throws an unchecked exception, * typically <tt>nullpointerexcepti On</tt> or <tt>classcastexception</tt>. * Attempting to query the presence of a ineligible key or value may throw a * exception, or it may simply return false; Some implementations would exhibit * the former behavior and some would exhibit the latter. More generally, * attempting a operation on a ineligible key or value whose completion * would not result in the Inserti On a ineligible element into the-the map may * Throw a exception or it may succeed, at the option of thE implementation. * Such exceptions is marked as "optional" in the specification for this * interface. * * <p>many methods in collections Framework interfaces is defined * In terms of the {@linkobject#equals (Object) equals} method. For * example, the specification for the {@link#containsKey (Object) * ContainsKey (Object key)} method says: "Returns <tt>true</tt> if and * only if" Map contains a mapping for a keys <tt>k</tt> such that * <tt> (key==null? K==null:key.equals (k)) </t T>. " This specification should * <i>not</i> is construed to imply that invoking <TT>MAP.CONTAINSKEY</TT&G T * With a non-null argument <tt>key</tt> would cause <tt>key.equals (k) </tt> to * is invoked for any Key <tt>k</tt>. Implementations was free to * implement optimizations whereby the <tt>equals</tt> invocation was avoided, * for Example, by first comparing the hash codes of the keys. (The * {@linkObject#hashcode ()} specification guarantees that the objects with * Unequal hash codes cannot is equal.) More generally, implementations of * the various collections Framework interfaces is free to take advantage of * the spec ified behavior of underlying {@linkObject} methods wherever the * implementor deems it appropriate. * * <p>some map operations which perform recursive traversal of the the map may fail * With a exception for Self-refere Ntial instances where the map directly or * indirectly contains itself. This includes the {@codeClone ()}, * {@codeequals ()}, {@codehashcode ()} and {@codetoString ()} methods. * Implementations may optionally handle the self-referential scenario, however * Most current implementations does not does so . * * <p>this interface is a member of the * <a href= "{@Docroot}/.. /technotes/guides/collections/index.html "> * Java collections Framework</a>. * * @param<K> The type of keys maintained by this map *@param<V> The type of mapped values * *@authorJosh Bloch *@seeHashMap *@seeTreeMap *@seeHashtable *@seeSortedMap *@seeCollection *@seeSet *@since1.2*/ Public InterfaceMap<k,v>
Map is an interface where a map cannot contain duplicate keys, and each key can only map a single value.
The map interface is used to replace the dictionary abstract class.
The map interface provides a collection of three collection views, a collection of 1.key collections of 2.value 3.key-value. The order of elements within a map depends on the implementation of the iterator, and getting the collection view is actually getting an iterator that hides the details of the traversed element. The TreeMap class guarantees the order in which elements are traversed, and HASHMAP cannot guarantee the order in which elements are traversed.
Note: When using a mutable object as a key, be careful that the map is based on the hashcode and the Equals method to decide where to store it. A special case is that a map does not allow itself as a key, but allows itself to be a value.
All multi-purpose map implementation classes should provide two "standard" constructors, an parameterless constructor used to create an empty map, a constructor with only one parameter, and a map, to create a new map with the same key-value mapping as the incoming parameter. In fact, the latter allows the copying of any map, which is only a recommendation and is not mandatory because the interface cannot contain constructors, but this recommendation is adhered to in the JDK.
If the operation of a method is not supported, this method specifies that the unsupportedoperationexception exception be thrown. If this operation has no effect on Mao, then it is also possible to not throw a unsupportedoperationexception exception. For example, in a map that cannot be modified, call the Putall (map) method, and if the map's mapping is empty, the unsupportedoperationexception exception is not required to be thrown.
The map interface is a member of the Java Collections Framework.
Map inside the method:
intsize ();//Returns the number of Key-value mappings in the mapBooleanisEmpty ()//If no Key-value map in map returns True
If map does not contain a key mapping, returns False when the type of key does not match, throws classcastexception when key is
Null and the map does not support the value of key is null when thrown NullPointerExceptionBooleanContainsKey (Object key);
If the map contains more than one key to map the parameter value, return True, the exception throws the same case as ContainkeyBooleanContainsvalue (Object value);
The corresponding value is obtained according to Key, if there is no corresponding mapping, returns NULL if map allows value to be null, returns
Null may be a mapping with a pair of key-null or no corresponding mapping
V get (Object key);
Put a pair of key-value mappings into the map v put (K key, v value);
Delete the corresponding map according to key v remove (Object key);
Copy a map with the same parametersvoidPutall (map<?extendsK?extendsV>m);
Clears all mappings in the mapvoidClear ();
Returns the collection set for all keys in the map<K>KeySet ();
Returns a collection of all the value in the Map collection<V>values ();
Returns the collection set of Key-value<map.entry<k, v>>EntrySet ();
Compare caller to parameter equalityBooleanequals (Object O);
Calculate hash code for mapinthashcode ();//There are other default methods ..., which are published by jdk1.8 .
The map interface has an internal interface entry<k,v> in fact, it is the data structure of map storing Key-value map.
InterfaceEntry<k,v> {
Returns the corresponding key K GetKey ();
Returns the corresponding value V getValue ();
Setting replaces the old value with the new value, the return value is the old value v SetValue (v value);
Returns true if the mapping of two entry is the sameBooleanEquals (Object O);
Calculate the hash code for entryinthashcode ();
The following static method is published by JDK1.8
Returns a comparator that compares the natural size of a key Public Static<kextendscomparable<?SuperK>, v> comparator<map.entry<k,v>>Comparingbykey () {return(Comparator<map.entry<k, V>> &Serializable) (c1, C2)-C1.getkey (). CompareTo (C2.getkey ());//Here is a lambda expression}
Returns a comparer that is the natural size of the value of the comparison rule Public Static<k, Vextendscomparable<?SuperV>> comparator<map.entry<k,v>>Comparingbyvalue () {return(Comparator<map.entry<k, V>> &Serializable) (c1, C2)-C1.getvalue (). CompareTo (C2.getvalue ()); }
Returns a comparator, with the comparison rule passed in with parameters, compared to key Public Static<k, v> comparator<map.entry<k, v>> comparingbykey (comparator<?SuperK>CMP) {objects.requirenonnull (CMP); return(Comparator<map.entry<k, V>> &Serializable) (c1, C2)-Cmp.compare (C1.getkey (), C2.getkey ()); }
Returns a comparer, with the comparison rule passed in with parameters, compared to value Public Static<k, v> comparator<map.entry<k, v>> comparingbyvalue (comparator<?SuperV>CMP) {objects.requirenonnull (CMP); return(Comparator<map.entry<k, V>> &Serializable) (c1, C2)-Cmp.compare (C1.getvalue (), C2.getvalue ()); }}
JAVA.UTIL.MAP Source Code Analysis