Map key-value pairs

Map key-value pairs

* a: add functionality
  V put (K key, V value ): add elements.
  * If the key is first stored, the direct memory element, return null
  * If the key is not the first time exist, replacing the previous value with the value out, returns the previous value
* b: delete function
  * void clear (): remove all the elements of the key-value
  * V remove (Object key): the key to delete the key element, and returns the value
* c: determining function
  * boolean containsKey (Object key): determining whether the collection contains a specified key
  * boolean containsValue (Object value): determining whether the collection contains a specified value
  * boolean isEmpty (): determines whether the set is empty
* d: obtaining function
  * the set <of Map.Entry <K, V >> the entrySet ():
  * V GET (Object key): obtained according to the key value
  * set <K> keySet () : Get the collection in the collection of all keys
  * collection <V> values () : Get the set of all values in the set
* e: length function
  * int size (): returns a collection of key-value pairs the number of

first traversal: keySet () to obtain the collection of key, to get through the corresponding get (key) value
second traverse: the entrySet () to obtain a set of key-value pairs:

public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("张三", 29);
        map.put("李四", 24);
        map.put("王五", 25); 
        map.put("赵柳", 26);
        /*Set<String> keySet = map.keySet();
        Iterator<String> iterator = keySet.iterator();
        while(iterator.hasNext()){     // 迭代器迭代
            String key = iterator.next();
            Integer value = map.get(key);
            System.out.println(value);*/
        for (String key : map.keySet()) {  
            System.out.println(map.get(key));
        }
        }
    }
public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("张三", 29);
        map.put("李四", 24);
        map.put("王五", 25); 
        map.put("赵柳", 26);
        
        /*Set<Map.Entry<String, Integer>> ets = map.entrySet();
        Iterator<Map.Entry<String, Integer>> iterator = ets.iterator();
        while (iterator.hasNext()) {
            //获取每个Entry对象
            Map.Entry<String, Integer> en = iterator.next();
            Key en.getKey = String ();
            Integer value = en.getValue();
            System.out.println (Key + ".." + value); 
            
        } * / 
     //
of Map.Entry <String, Integer> Map interface is the interface Entry
        for (Map.Entry<String, Integer> en : map.entrySet()) {
            String key = en.getKey();
            Integer value = en.getValue();
            System.out.println(key + ".." + value);
        }
        

 



 

Guess you like

Origin www.cnblogs.com/yaobiluo/p/11306295.html