Map Introduction
Map the key to the object of the value. A map cannot contain duplicate keys; each key can only map to one value at most. This interface replaces the Dictionary class, which is entirely an abstract class, not an interface.
The Map interface provides three collection views that allow viewing the contents of a map in the form of a key set, a value set, or a key-value mapping relationship set. The mapping order is defined as the order in which the iterator returns its elements on the map's collection view. Some mapping implementations explicitly guarantee their order, such as the TreeMap class; others do not guarantee their order, such as the HashMap class.
Note: Extra care must be taken when using mutable objects as mapping keys. When an object is a key in a map, if the value of the object is changed in a way that affects the equals comparison, the behavior of the map will be uncertain. A special case that this prohibits is that a map does not allow itself to be included as a key. Although a map is allowed to include itself as a value, be careful: the definition of equals and hashCode methods will no longer be explicit on such maps.
All common mapping implementation classes should provide two "standard" constructors: a void (no parameter) constructor for creating empty maps; and a constructor with a single Map type parameter to create a new map with the same key-value mapping relationship as its parameters. In fact, the latter constructor allows the user to copy arbitrary maps to generate an equivalent map of the required class. Although this suggestion cannot be enforced (because the interface cannot contain constructors), all common mapping implementations in the JDK follow it.
The "destroy" methods contained in this interface modify the mapping of its operations, and if this mapping does not support the operation, these methods will throw an UnsupportedOperationException. If so, then these methods can (but do not require) throw UnsupportedOperationException when the call is invalid for the mapping. For example, if an unmodified map whose mapping relationship is "overlapping" is empty, an exception can be thrown (but not required) when calling the putAll(Map) method to the map.
Some mapping implementations have limitations on the keys and values that may be included. For example, some implementations prohibit null keys and values, others have restrictions on the type of their keys. Attempting to insert an unqualified key or value will throw an unchecked exception, usually a NullPointerException or ClassCastException. Attempting to query whether an unqualified key or value may throw an exception, or return false; some implementations will exhibit the former behavior, while others will exhibit the latter. Generally speaking, when attempting to perform an operation on an unqualified key or value and the completion of the operation does not result in an unqualified element being inserted into the map, an exception may be thrown or the operation may be successful, depending on the implementation itself. Such exceptions are marked "optional" in the specification of this interface.
This interface is a member of the Java Collections Framework.
Many methods in the Collections Framework interface are defined according to the equals method. For example, the specification of the containsKey(Object key) method says: "Return true if and only if this map contains a mapping relationship for key k that satisfies (key==null ? k==null : key.equals(k)). This specification should not be interpreted as: calling Map.containsKey with a non-null parameter key will result in a call to key.equals(k) for arbitrary key k. Implementations can be optimized at will to avoid calling equals. For example, you can first compare the hash codes of two keys (the Object.hashCode() specification ensures that two objects whose hash codes are not equal will not be equal). Generally speaking, implementations of various Collections Framework interfaces can take advantage of the specified behavior of the underlying Object method as long as the implementer considers it appropriate.
Common operating instructions void clear()
Remove all mapping relationships from this map (optional action).
boolean containsKey(Object key)
Returns true if this map contains a mapping relationship for the specified key.
boolean containsValue(Object value)
Returns true if this map maps one or more keys to the specified value.
Set<Map.Entry<K,V>> entrySet()
Returns the Set view of the mapping relationships contained in this map.
boolean equals(Object o)
Compare whether the specified object is equal to this map.
V get(Object key)
Returns the value mapped by the specified key; if this map does not contain the mapping relationship of the key, return null.
int hashCode()
Returns the hash code value for this map.
boolean isEmpty()
Return true if this map does not contain a key-value mapping relationship.
Set<K> keySet()
Returns the Set view of the keys contained in this map.
V put(K key, V value)
Associate the specified value with the specified key in this map (optional action).
void putAll(Map<? extends K,? extends V> m)
Copy all mapping relationships from the specified mapping into this map (optional action).
V remove(Object key)
If there is a mapping relationship for a key, it is removed from this map (optional).
int size()
Returns the key-value mapping relationship in this map.
Collection<V> values()
Returns the Collection view of the values contained in this map.
General usage of Map
1. Declare a Map:
The code copy is as follows:
Map map = new HashMap();
2. Put the value into the map. Note: map is stored in the form of key-value, such as:
The code copy is as follows:
map.put("sa","dd");
3. Take the value from the map:
The code copy is as follows:
String str = map.get("sa").toString,
The result is: str = "dd'
4. Iterate through a map and get the key and value from it:
The code copy is as follows:
Map m= new HashMap();
for(Object obj : map.keySet()){
Object value = map.get(obj);
}