In the Last Post we introduced collection collection, http://zhangyh8856643.blog.51cto.com/8846643/1829364
In this blog post we will introduce the map collection.
First of all, the map and collection collection are an interface, the implementation of which is implemented by the Xiamen implementation of the class to achieve the function. The biggest difference is that collection is a single-column collection, and map is a double-row collection (that is, the generic parameter is a key-value pair). The map collection is similar to set, with the HashMap, TreeMap, and Hashtable three implementation classes, which are not used by Hashtable.
-map (interface)
-hashmap
-treemap
-hashtable
Here's how to get the value of HashMap in three HashMap.
Public class maptest { public static void main (String[] args) { hashmap<string, string> map=new hashmap <String, String> ( map.put); "010", "Beijing"); map.put (" 021 ", " Shanghai ") map.put (" 012 ", " TianJin "); map.put ("043", "ChongQing"); map.put ("026", "GuangZhou"); //method One: The key is obtained by Map.keyset () method set <string> zips=map.keYset ();//Get Key iterator<string> it=zips.iterator (); while (It.hasnext ()) { string zip=it.next (); system.out.println (zip+ ":" +map.get (Zip)); } // Method Two: By Map.values () method to get its value collection<string> cities=map.values ();//Get value iterator<string> it=cities.iterator (); while (It.hasnext ()) { System.out.println (It.next ()); //method Three: Get the key value by Map.entryset () (that is, an entry entry) Set<Entry<String, String>> Entries=map.entrYset ();//Get key value pair iterator<entry<string, string>> it= Entries.iterator (); while (It.hasnext ()) { entry<string, string> entry=it.next (); String key=entry.getkey (); string value= Entry.getvalue (); system.out.println (key+ ":" +value); }}
The above HashMap can be modified to TreeMap, and other code can be output sequentially without modification.
This article is from the "Java to school on Java" blog, please be sure to keep this source http://zhangyh8856643.blog.51cto.com/8846643/1829368
Java Collection Framework (ii)