HashMap four kinds of traversal

    public static void main(String[] args){
        Map<String,String> map = new HashMap<String, String>();
        map.put("1", "value1");
        map.put("2", "value2");
        map.put("3", "value3");
        
        //第一种:普遍使用
        System.out.println("通过map.keySet遍历key和value");
        for(String key :map.keySet()){
            System.out.println("key= "+key+"  value= "+map.get(key));
        }
        
        //第二种,通过map.entrySet()
        System.out.println ( "iterator traversal through the use of key and value EnumMap.entrySet" ); 
        the Iterator <the Entry <String, String >> ITE = EnumMap.entrySet () iterator ();.
         The while (ite.hasNext ()) { 
            the entry <String, String> = entry ite.next (); 
            System.out.println ( "Key =" entry.getKey + () + "value =" + entry.getValue ()); 
        } 
        
        // third party: recommended especially when large capacity 
        System.out.println ( "key and traversing through EnumMap.entrySet value" );
         for (the entry <String, String> entry: EnumMap.entrySet ()) { 
            System.out.println ( "key = "+ entry.getKey () +"value= "+entry.getValue());
        } 
        
        Fourth: Use map.values () but not through all of the traversal value Key 
        System.out.println ( "map.values by traversing all value" );
         for (String V: map.values ()) { 
            the System. Out.println ( "value =" + V); 
        } 
    }//

 

Guess you like

Origin www.cnblogs.com/zengcongcong/p/11294848.html