1, What is it?
- Interface map is a container accessed by key value pairs. Map < key, value > k represents key, and V represents value.
- It is an interface, so its implementation class should be used when using it. The commonly used ones are HashMap,Hashtable,LinkedHashMap, etc. All interfaces and implementation classes are as follows:
2, Common interfaces.
All the interfaces of Map are as follows: open eclipse to see the implementation class of Map, and you can see all the implementation interfaces in the outline window. Arrows are the most commonly used interfaces for foundations.
- keyset() is to get all the key s
- values() is to get all the values
- Entry set (k, V) is to store the key value pairs in the Map as entry objects in the set set.
3, Traversal in Map set
A:Map set traversal method keySet method
1. Get all the keys in the Map Set. Since the keys are unique, a Set set Set is returned to store all the keys
2. Traverse the Set set of keys to get each key
3. Use get(key) to find the corresponding value in the Map according to the key
public class Demo1 {
public static void main(String[] args) {
System.out.println("=======================1");
/*
* 1. Call the method keySet of the map collection, and all keys are stored in the Set collection
* 2. Traverse the Set set to get all the elements in the Set set (the key in the Map)
* 3. Call the map collection method get to get the value through the key
*/
Map<String,Integer> map = new HashMap<String,Integer>();
map.put("a", 11);
map.put("b", 12);
map.put("c", 13);
map.put("d", 14);
//1. Call the method keySet of the map Set, and all keys are stored in the Set set
Set<String> set = map.keySet();
//2. Traverse the Set set to get all the elements in the Set set (the key in the Map)
Iterator<String> it = set.iterator();
while(it.hasNext()){
//it.next returns the Set set Set element, which is the key in the Map
//3. Call the map collection method get to get the value through the key
String key = it.next();
Integer value = map.get(key);
System.out.println(key+"...."+value);
}
System.out.println("=======================2");
for(String key : map.keySet()){
Integer value = map.get(key);
System.out.println(key+"...."+value);
}
System.out.println("=======================3");
Demo1.ergodicMap(map.entrySet());
}
public static void ergodicMap(Set<Entry<String, Integer>> entry){
for(Entry<String, Integer> e: entry){
System.out.println(e.getKey()+"...."+e.getValue());
}
}
}