
They reject duplicate keys at creation time.They are serializable if all keys and values are serializable.Null keys or values result in NullPointerException. Map to behave inconsistently or its contents to appear to change. However, if the contained keys or values are themselves mutable, this may cause the Will always cause UnsupportedOperationException to be thrown. Instances created by these methods have the following characteristics:Ĭannot be added, removed, or updated. Static factory methods provide a convenient way to create unmodifiable maps. Most current implementations do not do so. Implementations may optionally handle the self-referential scenario, however This includes the clone(),Įquals(), hashCode() and toString() methods. With an exception for self-referential instances where the map directly or Some map operations which perform recursive traversal of the map may fail The specified behavior of underlying Object methods wherever the
.get method map java free#
The various Collections Framework interfaces are free to take advantage of Unequal hash codes cannot be equal.) More generally, implementations of Object.hashCode() specification guarantees that two objects with Implement optimizations whereby the equals invocation is avoided,įor example, by first comparing the hash codes of the two keys. With a non-null argument key will cause key.equals(k) toīe invoked for any key k. Not be construed to imply that invoking Map.containsKey (key=null ? k=null : key.equals(k))." This specification should Only if this map contains a mapping for a key k such that Many methods in Collections Framework interfaces are definedĮxample, the specification for the containsKey(Object key) method says: "returns true if and Such exceptions are marked as "optional" in the specification for this Throw an exception or it may succeed, at the option of the implementation. Would not result in the insertion of an ineligible element into the map may More generally,Īttempting an operation on an ineligible key or value whose completion The former behavior and some will exhibit the latter. Typically NullPointerException or ClassCastException.Īttempting to query the presence of an ineligible key or value may throw anĮxception, or it may simply return false some implementations will exhibit To insert an ineligible key or value throws an unchecked exception, Values, and some have restrictions on the types of their keys. For example, some implementations prohibit null keys and Some map implementations have restrictions on the keys and values they Method on an unmodifiable map may, but is not required to, throw theĮxception if the map whose mappings are to be "superimposed" is empty.

To, throw an UnsupportedOperationException if the invocation would If this is the case, these methods may, but are not required UnsupportedOperationException if this map does not support the


Methods that modify the map on which they operate, are specified to throw The "destructive" methods contained in this interface, that is, the There is no way toĮnforce this recommendation (as interfaces cannot contain constructors) butĪll of the general-purpose map implementations in the JDK comply. Producing an equivalent map of the desired class. In effect, the latter constructor allows the user to copy any map, Which creates a new map with the same key-value mappings as its argument. "standard" constructors: a void (no arguments) constructor which creates anĮmpty map, and a constructor with a single argument of type Map, Permissible for a map to contain itself as a value, extreme caution isĪdvised: the equals and hashCode methods are no longerĪll general-purpose map implementation classes should provide two Is not permissible for a map to contain itself as a key. A special case of this prohibition is that it The behavior of a map is not specified if the value of an object isĬhanged in a manner that affects equals comparisons while the Note: great care must be exercised if mutable objects are used as map Specific guarantees as to their order others, like the HashMap Some map implementations, like the TreeMap class, make The order in which the iterators on the map's collection views return theirĮlements. The Map interface provides three collection views, whichĪllow a map's contents to be viewed as a set of keys, collection of values, Was a totally abstract class rather than an interface. This interface takes the place of the Dictionary class, which
