A brief introduction to the use of maps in Java
- 2020-04-01 04:12:18
- OfStack
Public interface Map< K, V>
Objects that map keys to values. A map cannot contain duplicate keys; Each key can map to at most one value.
import javautilHashMap;
import javautilMap;
public class Test {
public static void main(String[] args) {
Map map = new HashMap();//Declare a Map
mapput("s", " Ha ha ");//Put a value in a map: a map is stored as key-value
String str = mapget("s")toString();//Map gets the value of key "s"
Systemoutprintln(str);
}
}
Output: haha
Map key-value pairs, where values are typically stored as objects.
Common method in hashmap, put(object key,object value); Associates the specified value with the specified key in this map
Get (object key); // find the corresponding value according to the key value.
Check whether the key exists: containsKey(object key)
Check whether the value exists: containsValue(object value)
The feature of a Map is key-value matching
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) {
Map map = new HashMap();
String key = "java";
String key = "java";
map.put(key, "java The value of the ");
map.put(key, "java The value of the ");
System.out.println(map.get(key));
System.out.println(map.get(key));
}
}
Output:
The value of the java1
The value of the java2