Introduction
The Javaa collection framework provides a Map interface specially used to handle the storage of key-value mapping data. Multiple elements can be stored in a Map, and each element is composed of two or two objects, that is, a key (key) object and a value (value) object, and the corresponding value can be mapped according to the key.
-
Elements are stored in a map of key-value pairs, where the key is the keyword of the element and the value is the actual element.
-
The key-value mapping is managed by the Entry object of the internal interface of Map
-
The key cannot be repeated, once it is repeated, the previous key-value pair mapping will be overwritten
-
Keys are saved using Set and values are saved using Collection
-
There is no inheritance relationship with the Collection interface
Map implementation class
HashMap:The key cannot be repeated, and the order of saving and adding is irrelevant
TreeMap: Keys cannot be repeated, cannot be Null, and the storage order is in natural order (requires that the key object must implement the Comparable interface, and the key type must be consistent)
Hashtable: thread safe, key cannot be repeated, cannot be null;
ConcurrentHashMap is thread-safe and can guarantee performance, it cannot be null;
import java.util.*; import java.util.Map.Entry; //Introduce the internal interface of Map import java.util.concurrent.ConcurrentHashMap; class ETest{ public static void main(String[] arg){ Map m01 = new HashMap(); m01.put("a1","Huaan"); m01.put("a11",3.1415926); m01.put("a2",0.618); m01.put(null,"What are you worried about?"); System.out.println("1...length:" + m01.size() ); System.out.println("1...element:" + m01 ); m01 = new TreeMap(); m01.put("a1","Huaan"); m01.put("a11",3.1415926); m01.put("a2",0.618); //m01.put(null,"What are you worried about?"); System.out.println("2...length:" + m01.size() ); System.out.println("2...element:" + m01 ); m01 = new Hashtable(); m01.put("a1","Huaan"); m01.put("a11",3.1415926); m01.put("a2",0.618); //m01.put(null,"What are you worried about?"); System.out.println("3...length:" + m01.size() ); System.out.println("3...elements:" + m01 ); m01 = new ConcurrentHashMap(); m01.put("a1","Huaan"); m01.put("a11",3.1415926); m01.put("a2",0.618); //m01.put(null,"What are you worried about?"); System.out.println("4...length:" + m01.size() ); System.out.println("4...elements:" + m01 ); }//main(); }
Store key-value pair elements
key: order is not required, duplication is not allowed value: order is not required, but duplication is allowed
A Map object consists of several “key-value pairs”
High efficiency in querying specified elements
Common methods
Object put(key,value) |
Establish a key-value pair mapping relationship and save it in the map collection |
|
void putAll(Map) |
Merge all key-value pairs in another Map collection |
|
int size |
collection length |
|
Object get(key) |
Get the value of the specified key |
|
Object remove(key) |
Delete the key-value pair mapping corresponding to the specified key |
|
void clear() |
Delete all elements in the Map object |
|
boolean containsKey(object) |
Whether the key exists (satisfying the general contract of hashCode overrides equsls+hashCode) |
|
boolean containsValue(Object) |
Does the value exist |
|
Collection values() |
Get all the value values in the map |
|
Set keySet() |
Get all Key values |
|
Set entrySet() |
Get all the elements in the map, k1=v1, k2=v2,….. output in the form |
|
boolean isEmpty() |
Determine whether the collection is empty, return true if empty |
Common method code example:
operation result:
print result