Java Map learning and javamap Learning
I. Common APIs:
| Clear () |
Delete all mappings from Map |
| Remove (Object key) |
Delete keys and associated values from Map |
| Put (Object key, Object value) |
Associate a specified value with a specified key |
| PutAll (Map t) |
Copy all mappings in the specified Map to this map |
| EntrySet () |
Returns the Set view of the ing contained in the Map. Each element in the Set is a Map. Entry object. You can use the getKey () and getValue () methods (and the setValue () method) to access the key elements and value elements of the latter. |
| KeySet () |
Returns the Set view of keys contained in the Map. If you want to delete the elements in the Set, the corresponding ing (key and value) in the Map will also be deleted) |
| Values () |
Returns the Collection view of values contained in the map. If you want to delete the elements in the Collection, the corresponding ing (key and value) in the Map will also be deleted) |
| Get (Object key) |
Returns the value associated with the specified key. |
| ContainsKey (Object key) |
Returns true if Map contains the ing of the specified key. |
| ContainsValue (Object value) |
Returns true if the Map maps one or more keys to a specified value. |
| IsEmpty () |
Returns true if Map does not contain key-value ing. |
| Size () |
Returns the number of key-value mappings in the Map. |
2. A map operation:
1 // Map declaration, initialize 2 Map <String, Object> map1 = new HashMap <> (); 3 Map <String, Object> map2 = new HashMap <> (); 4 Map <String, Object> map3 = new HashMap <> (); 5 // put operation 6 map1.put ("name", "xiaoming"); 7 map1.put ("sex ", "0"); 8 map1.put ("phone", "13566668888"); 9 map1.put ("email", "xiaoming@163.com"); 10 map1.put ("age ", "18"); 11 // get operation 12 Object phone = map1.get ("phone"); 13 // keySet operation 14 for (String key: map1.keySet ()) {15 map2.put (key, map1.get (key); 16} 17 // putAll operation 18 map3.putAll (map1); 19 int size = 0; 20 // containsKey operation 21 if (map2.containsKey ("name") {22 // containsValue operation 23 if (map3.containsValue ("xiaoming ")) {24 // remove operation 25 map1.remove ("sex"); 26 // size operation 27 size = map1.size (); 28} 29} 30 // clear operation 31 map2.clear (); 32 // imEmpty operation 33 if (map2.isEmpty () {34 map2.putAll (map); 35}
1 // entrySet operation. Note the entry type 2 for (Map. entry <String, Object> entry: map3.entrySet () {3 System. out. println (entry. getKey () + ":" + entry. getValue (); 4}
Reference: http://baike.xsoftlab.net/view/250.html#1_1