catalogue
1: Introduction to Map interface
5: Map container union operation
7: HashMap_ Determine whether key or value exists
1: Introduction to Map interface
The Map interface defines the storage characteristics of the two column set. It is not a sub interface of the Collection. The two column set is stored with the characteristics of Key and Value (similar to the combination in Mathematics)
● the elements in the Map exist in pairs, similar to couples in the society, while the elements in the Collection exist individually, similar to single dogs in the society
● commonly used containers in Map include HashMap and TreeMap
2: Common methods in Map
3: HashMap_ Add element
HashMap is the implementation class of Map interface, which is implemented by hash algorithm. Duplicate keys are not allowed. If there are duplicate new values, they will replace the old values. HashMap is very efficient in finding, deleting and adding
V put(K,Key ,V value)
If the same Key appears, the new Key will replace the old Key and return the Value corresponding to the old Key
If the Key appears for the first time, null will be returned
code
import java.util.HashMap; import java.util.Map; public class HashMapTest { public static void main(String[] args) { //Instantiate HashMap Map<String,String> map=new HashMap<>(); //Add element String str=map.put("Chen Shuchang","Greater Songbi"); System.out.println(str);//Null will be returned because if there is no corresponding Key in the Map, null will be returned. If there is, the same Value will be returned String str2=map.put("Chen Shuchang","Little counsellor"); System.out.println(str2); } }
4: HashMap_ Get element
The first method
V get(K key) obtains the corresponding Value through the corresponding Key. If the Key does not exist, it will return null
code
//Get element System.out.println("-------Get element-------------"); //Get the corresponding Value through the Key System.out.println(map.get("Chen Shuchang")); System.out.println(map.get("ha-ha"));
The second method
Set keySet() is often used with the get method
Return the Key in the Map container in the form of Set set (return a Set container)
code
System.out.println("--------------keySet--------"); //Key Set gets all the keys in the Map Set and puts them into the Set set //Get all the elements in the HashMap, and the KeySet gets the elements together with get //Instantiate a Set container Set<String> set=map.keySet(); map.put("Lu Xinyu","Handsome force!"); map.put("Lu Xiaoyu","Handsome force!!"); map.put("Lu Dayu","Handsome!!!"); for(String str1:set){ String str3=map.get(str1); System.out.println(str1+"------->"+str3); }
The third method
Set < Map.Entry < K, V > > entryset() returns a set based on all the Value values in the Map set of type Map.Entry
code
System.out.println("----------entrySet----------"); Set<Map.Entry<String,String>> entries=map.entrySet(); //The Entry interface is defined inside the Map interface. Call the Entry interface, Map.Entry //There are getKey and getValue methods in the Entry type for(Map.Entry<String,String> entrys:entries){ String key= entrys.getKey(); String val=entrys.getValue(); System.out.println(key+"----->"+val) ; }
5: Map container union operation
void putAll(Map M);
Merge elements from another container into this container. If the merged Key is the same as the Key value of the original container, it will replace the Key of the original container
code
System.out.println("----------Map Consolidation of containers----------"); Map<String ,String> map2=new HashMap<>(); map2.put("Lu Xinyu","Dashuaibi"); map2.putAll(map); Set<String> set3=map2.keySet(); for(String str4:set3){ System.out.println("key:"+str4+"value:"+map2.get(str4)); }
6: HashMap_ Delete element
V remove(Object key) delete the corresponding element through the key and return the Value of the deleted element
code
7: HashMap_ Determine whether key or value exists
boolean containsKey(Object Key)
boolean containsValue(Object Value)