

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
