Five ways to set up a Java traversal map method

Source: Internet
Author: User

The following is a copy of other people's diary, to facilitate their later use, only for their own learning.

Package com.jackey.topic;

Import java.util.ArrayList; Import Java.util.HashMap; Import Java.util.Iterator; Import java.util.List; Import Java.util.Map; Import Java.util.Set;

Method of looping through a Map public class Circlemap { public static void main (string[] args) {  map<string, integer> tem PMap = new hashmap<string, integer> ();   Tempmap.put ("a", 1);   Tempmap.put ("B", 2);   Tempmap.put ("C", 3);  //JDK1.4  //Traversal method one HashMap EntrySet () traversal   SYSTEM.OUT.PRINTLN ("Method One");   Iterator it = Tempmap.entryset (). Iterator ();   while (It.hasnext ()) {   Map.entry Entry = (map.entry) it.next ();    Object key = Entry.getkey ();    Object value = Entry.getvalue ();    System.out.println ("key=" + key + "value=" + value);  }   System.out.println ("");  //JDK1.5, apply new features For-each loop  //Traverse method two   System.out.println ("Method two");   for (map.entry<string, integer> entry:tempMap.entrySet ()) {   String key = Entry.getkey (). Tostri Ng ();    String value = Entry.getvalue (). toString ();    System.out.println ("key=" + key + "value=" +Value);  }   System.out.println ("");

Traverse method Three HashMap KeySet () Traverse System.out.println ("method three"); for (Iterator i = Tempmap.keyset (). Iterator (); I.hasnext ();)    {Object obj = I.next ();   System.out.println (obj);//Loop Output key System.out.println ("key=" + obj + "value=" + tempmap.get (obj)); } for (Iterator i = tempmap.values (). Iterator (); I.hasnext ();)    {Object obj = I.next (); System.out.println (obj);//loop Output value} System.out.println ("");

Traverse method Four TreeMap KeySet () Traverse System.out.println ("method four");   For (Object O:tempmap.keyset ()) {System.out.println ("key=" + O + "value=" + tempmap.get (o)); } System.out.println ("11111");

How Java traverses map <string, arraylist> map = new HashMap <string,//arraylist> (); System.out. println ("Java traversal map <string, arraylist> map = new HashMap

<string, arraylist> (); ");   map<string, arraylist> map = new hashmap<string, arraylist> ();   set<string> keys = Map.keyset ();   iterator<string> Iterator = Keys.iterator ();    while (Iterator.hasnext ()) {String key = Iterator.next ();    ArrayList ArrayList = Map.get (key);    for (Object o:arraylist) {System.out.println (O + "traversal process");   }} System.out.println ("2222");   map<string, list> maplist = new hashmap<string, list> ();    For (Map.entry Entry:mapList.entrySet ()) {String key = Entry.getkey (). toString ();    list<string> values = (List) entry.getvalue ();    for (String value:values) {System.out.println (key + "-+" + value); }   }  } }

Five ways to set up a Java traversal map method

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.