April 18, 2018JAVA

 The Map interface is not an inheritance of the Collection interface. The Map interface is used to maintain key/value pairs. This interface describes a mapping from unique keys to values.
(1) Add and delete operations:
Object put(Object key, Object value): Put a key and a value associated with each other into the image. If the key already exists, the new value associated with this key will supersede the old value. The method returns the old value of the keyword, or null if the keyword didn't exist before
Object remove(Object key): remove the key-related mapping from the image
void putAll(Map t): adds all elements from a specific map to this map
void clear(): removes all mappings from the image
"Both keys and values ​​can be null. However, you cannot add a Map to itself as a key or value."
(2) Query operation:
Object get(Object key): Get the value associated with the key key and return the object associated with the key key, or null if the key is not found in the image
boolean containsKey(Object key): Determines whether the keyword key exists in the image
boolean containsValue(Object value): Determines whether the value value exists in the image
int size(): returns the number of maps in the current image
boolean isEmpty() : Determine if there are any mappings in the image
(3) View operations: process key/value pairs in the image
Set keySet(): Returns a viewset of all keys in the image
"Because the set of keys in the map must be unique, you back it with Set. You can also remove elements from the view, and at the same time, the key and its associated value will be removed from the source image, but you can't add any elements. "
Collection values(): Returns a viewset of all values ​​in the image
"Because the set of values ​​in the map is not unique, you support it with Collection. You can also remove elements from the view, and at the same time, the value and its key will be removed from the source image, but you cannot add any elements."
Set entrySet(): Returns a viewset of Map.Entry objects, i.e. key/value pairs in the image
"Because the mapping is unique, you back it with Set. You can also remove elements from the view, and at the same time, those elements will be removed from the source image, but you can't add any elements."
 Map.Entry interface
The entrySet() method of Map returns a collection of objects that implement the Map.Entry interface. Each object in the collection is a specific key/value pair in the underlying Map.
With this collection's iterator, you can get the key or value of each entry (the only way to get it) and make changes to the value. After the entry is returned through the iterator, unless it is the remove() method of the iterator itself or the setValue() method of the entry returned by the iterator, other modifications to the outside of the source Map will cause the entry set to become invalid, and the entry behavior will be generated. undefined.
(1) Object getKey(): Returns the key of the entry
(2) Object getValue(): Returns the value of the item

(3) Object setValue(Object value): Change the value in the related image to value and return the old value

 

 

Map<String,Integer> map=new HashMap<String,Integer>();     
map.put("1", 1);     
map.put("2", 2);     
map.put("3", 3);     
map.put("3", 3);         
Iterator itor=map.entrySet().iterator();     
while(itor.hasNext()){     
  Map.Entry<String,Integer> entry=(Map.Entry<String,Integer>)itor.next();     
  System.out.println("key="+entry.getKey().toString());     
  System.out.println("values="+entry.getValue().toString());     
}   

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326829942&siteId=291194637