Several methods of traversing map in Java

Source: Internet
Author: User

transferred from : http://blog.csdn.net/wzb56/article/details/7864911

Methods are divided into two categories:

A class is a map-based entry;map.entryset ();

A class of map-based Key;map.keyset ()

There are two ways to traverse each class:

A. Using an iterator iterator;

B. Use of the For-each cycle;

Code examples are as follows

Package Cn.wzb;import Java.util.arraylist;import java.util.hashmap;import java.util.iterator;import java.util.List; Import Java.util.map;import Java.util.set;import java.util.map.entry;//looping through a Map public class TestMap {public static void Main (string[] args) {map<string, integer> Map = new hashmap<string, integer> (); Map.put ("A", 1); Map.put ( "B", 2); Map.put ("C", 3);//JDK1.4 in//traversal method one Hashmap.entryset () Traversal System.out.println ("method One");iterator<entry< String, integer>> it = Map.entryset (). iterator (); while (It.hasnext ()) {map.entry<string, integer> Entry = it . Next (); String key = Entry.getkey (); Integer value = Entry.getvalue (); System.out.println ("key =" + key + "; Value = "+ Value";} System.out.println ("");//JDK1.5, apply the new feature For-each loop//Traverse method Two System.out.println ("Method Two"); for (Map.entry<string, Integer> Entry:map.entrySet ()) {String key = Entry.getkey (); String value = Entry.getvalue (). toString (); System.out.println ("key =" + key + "value =" + value);} System.out.prIntln ("");//Traversal Method three Hashmap.keyset (). Iterator () Traverse System.out.println ("method three"); for (iterator<string> i = Map.keyset (). iterator (); I.hasnext (); ) {String key = I.next (); System.out.println ("key =" + key + ";   Value = "+ map.get (key));} /*for (Iterator<integer> i = map.values (). Iterator (); I.hasnext ();) {Integer value = I.next (); System.out.println (value));//Loop Output value}*//*for (Integer value:map.values ()) {System.out.println ("value =" + value);} */system.out.println ("");//Traverse Method four Hashmap.keyset () Traverse System.out.println ("method Four"); for (String Key:map.keySet ()) { System.out.println ("key =" + key + "; Value = "+ map.get (key));} System.out.println ();//How Java traverses map <string, arraylist> map = new HashMap <string, arraylist<integer> > (); SYSTEM.OUT.PRINTLN ("Java traversal map <string, arraylist<integer> map = new HashMap <string, arraylist<integer& Gt;> (); "); System.out.println ("Method one:"); Map<string, list<integer> > maplist = new HashMap <string, LIst<integer> > (); list<integer> list = new arraylist<integer> (); List.add (1); List.add (2); List.add (3); List.add (4); List.add (5); Maplist.put ("AAA", list); Maplist.put ("BBB", list); set<string> keys = Maplist.keyset ();iterator<string> Iterator = Keys.iterator (); while (Iterator.hasnext () ) {String key = Iterator.next (); SYSTEM.OUT.PRINTLN (key + ":"); list<integer> arrayList = Maplist.get (key), for (Integer i:arraylist) {System.out.print (i + ",");} System.out.println ();} System.out.println ("Method two:"); for (map.entry<string, list<integer> > Entry:mapList.entrySet ()) {String key = Entry.getkey (); SYSTEM.OUT.PRINTLN (key + ":"); list<integer> values = (list<integer>) entry.getvalue (); for (Integer value:values) {System.out.print ( Value + ",");} System.out.println ();}}}

  

Several methods of traversing map in Java

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.