Java Collection Framework (ii)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.