. 1 // the Map Key value is not the same, value may be the same value 2 // the Entry HashMap objects are randomly oriented . 3 . 4 // instantiate. 1 . 5 the Map <String, String> Maps = new new HashMap <> (); . 6 // instantiate 2 . 7 the Map <String, String> = entends new new the HashMap <> (); . 8 . 9 // additive elements 10 maps.put ( "A", "Joe Smith" ); . 11 maps.put ( "b" "John Doe" ); 12 is maps.put ( "C", "Wang Wu" ); 13 is entends.put ( "D","Zhao Si orphan" ); 14 entends.put ( "pent", "king pock" ); 15 16 // instantiate an absorbent of Example 2 . 17 maps.putAll (entends); // presence will remain the original value 18 is . 19 @ added element Return value 20 is String str1 = maps.put ( "cyclohexyl", "Wed" ); 21 is String str2 maps.put = ( "C", "Wang Wu" ); 22 is System.out.println (str1); // null (formerly absent output null) 23 is System.out.println (str2); // Wang Wu (existing output) 24 25 // traversal key and value 26 is for (of Map.Entry <String, String> entry: Maps. entrySet()) { 27 System.out.println("key:" + entry.getKey() + ";value:" + entry.getValue()); 28 } 29 30 // foreach遍历(Java8新特性) 31 maps.forEach((k, v) -> System.out.println("key:" + k + ";value:" + v)); 32 33 // 遍历key值1 34 for(String key:maps.keySet()){ 35 System.out.println("key:"+key); 36 } 37 38 // 遍历key值2 39 maps.keySet().forEach(k-> System.out.println("key:"+k)); 40 41 //Value traversal value. 1 42 is for (String value: maps.values ()) { 43 is System.out.println ( "value:" + value); 44 is } 45 46 is // iterate value value 2 47 . Maps.values () forEach (V-> System.out.println ( "value:" + V)); 48 49 // query element 50 String maps.get STR = ( "D" ); 51 is 52 is // delete the element 53 is maps.remove ( " D " ); 54 is 55 // Clear 56 is maps.clear (); 57 is System.out.println (" D: "+ str);
Reference Bowen: https://www.cnblogs.com/jpwz/p/5680494.html