Map<k,v>
The object that maps the key to a value. A map cannot contain duplicate keys, and each key can be mapped to at most one value.
1 ImportJava.util.HashMap;2 ImportJava.util.Map;3 4 5 Public classTest1 {6 7 Public Static voidMain (string[] args) {8Map map =NewHashMap ();//declaring a map9Map.put ("s", "haha");//put values in map: Map is stored in the form of Key-valueTenString str = map.get ("s"). ToString ();//map Gets the value of key "s" One System.out.println (str); A } -}
Output: haha
Map key-value pairs, where values are typically stored as objects.
HashMap common method, put (object Key,object value); Associates the specified value with the specified key in this map
Get (object key);//Find the corresponding value value based on the key value.
Determine if key exists: ContainsKey (object key)
Determine if the value exists: Containsvalue (object value)
The attribute of map is "key-value" (Key-value) match
1 ImportJava.util.HashMap;2 ImportJava.util.Map;3 4 5 6 Public classTest1 {7 8 Public Static voidMain (string[] args) {9Map map =NewHashMap ();TenString key1 = "JAVA1"; OneString Key2 = "Java2"; AMap.put (Key1, "value of JAVA1"); -Map.put (Key2, "value of Java2"); - System.out.println (Map.get (Key1)); the System.out.println (Map.get (Key2)); - } -}
Output:
The value of the JAVA1
The value of the JAVA2
Usage of map in Java (Haspmap usage)