当前位置:网站首页>Map interface and its sub implementation classes

Map interface and its sub implementation classes

2022-06-23 08:18:00 Liang FuFu

1. Map Interface

1.(1)Map<K,V> aggregate : An entity class that can describe things in the real world 
  (2) Objects that map keys to values , Represents a series of key value pairs 
  (3)Map Valid for key , Independent of value , Values can be repeated , But the key must be unique 
2. Subclass implementation :
		(1)HashMap: Implementation based on hash table Map Interface 
				    characteristic : allow null Values and null Key exists , Thread unsafe class , There is no guarantee that the sequence of iterations will never change 
		(2)TreeMap: At the bottom is a red black tree structure 

1.1 Map Basic functions of

1.   To obtain the length of the :
	public int size(): Get the number of collection elements 
     Add functionality :
    public V put(K key,V value): Add key value pair elements , There is a return value : If key No repetition , The return value is null, Whether the return value is null Determine whether to add repeatedly . since Object Of hashCode() and equals() Method 
     Delete function :
    public V remove(Object key): Delete specified key , And return the deleted value 
    public void clear(): Violent deletion , Clear all key value pair objects 
     Judgment function :
	public boolean containsKey(Object key): Determine whether the specified key is included 
    public boolean containsValue(Object value): Determine whether to include the specified value 
    public boolean isEmpty(): Determines if the set is empty     

1.2 Map The traversal of a set

1. Mode one : Get the set of all keys 
		public Set<K> keyset(): Get all the keys 
    	public V get (Object key): Get the value of the key 
2. Implementation code :
public class MapDemo{
    
    public static void main(String[] args){
    
        Map<String,String> map = new HashMap<>();
        map.put(" Yang2 guo4 "," Little dragon female ");
        map.put(" Guo Jing "," Huang Rong ");
        map.put(" Erkang "," Crape myrtle ") ;
        map.put(" Chen Xuanfeng "," Mei Chaofeng ") ;
        map.put(" Jia Baoyu " ," Lin daiyu ") ;
        // Get the set of all keys 
        Set<String> setKey = map.keySet();
        // Get all keys , Traverse Set aggregate 
        for(String key:setKet){
    
            String value = map.get(key);
            sout(key+"\t"+value);
        }
    }
}

3. Mode two : Get all key value pair objects 
    	Set<Map.Entry<K,V>> entrySet(); Get all key value pair objects , A key value pair object is called a mapping item 
    	 Use the methods in the interface :
						K getKey(): Get key 
                        V getValue(): Get value 
4. Implementation code :
public class MapDemo{
    
    public static void main(String[] args){
    
        Map<String,String> map = new HashMap<>();
        map.put(" Yang2 guo4 "," Little dragon female ") ;
        map.put(" Guo Jing "," Huang Rong ") ;
        map.put(" Erkang "," Crape myrtle ") ;
        map.put(" Chen Xuanfeng "," Mei Chaofeng ") ;
        map.put(" Jia Baoyu " ," Lin daiyu ") ;
        map.put(" linghu chong " ," Ren Yingying ") 
            
        Set<Map.Entry<String,String>> entry = map.entrySet();
        
        for(Map.Entry<String,String> en:entry){
    
            String key = en.getKey();
            String value = en.getValue();
            sout(key+"\t"+value);
        }
    }
}
    

2.HashMap aggregate

1. If the key is jdk Type of offer , You don't have to rewrite it hashCode() and equals()
   Custom types need to override both methods 

3.TreeMap aggregate

 Two ways of sorting :
			1. Realization Comparabele Interface , Rewrite one of compareTo Method 
            2. Realization Comparator Interface , rewrite compare Method 
原网站

版权声明
本文为[Liang FuFu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230745478567.html