public interface Map<K,V>
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.
import javautilHashMap; import javautilMap; public class Test { public static void main(String[] args) { Map map = new HashMap();//Declare a Map map ut("s", "haha");//Into the map Put value: map is a String stored in the form of key-value str = mapget("s")toString();//map gets the value with key "s" Systemoutprintln(str); } }
Output: Haha
Map key-value pairs, the values are generally stored as objects.
A commonly used method in hashmap, put(object key,object value); associates the specified value with the specified key in this map.
get(object key);//Find out the corresponding value based on the key value.
Determine whether the key exists: containsKey(object key)
Determine whether the value exists: containsValue(object value)
The feature of Map is "key-value" (Key-Value)
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 value"); map.put(key, "java value"); System.out.println(map.get(key)); System.out.println(map.get (key)); } }
Output:
The value of java1
The value of java2