Java HashMap method of looping through a map (turn)

Source: Internet
Author: User
Import java.util.ArrayList;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Map;
Import Java.util.Set;


A method of looping through a map
public class Circlemap {
public static void Main (string[] args) {
map<string, integer> tempmap = new hashmap<string, integer> ();
Tempmap.put ("A", 1);
Tempmap.put ("B", 2);

Tempmap.put ("C", 3);


In 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, the application of new characteristics For-each cycle
Traversal method Two
System.out.println ("Method II");
For (map.entry<string, integer> entry:tempMap.entrySet ()) {
String key = Entry.getkey (). toString ();
String value = Entry.getvalue (). toString ();
System.out.println ("key=" + key + "value=" + value);
}

System.out.println ("");


Traversal method three HashMap keyset () traversal
System.out.println ("Method III");
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 ("");


Traversal method four TreeMap keyset () traversal
System.out.println ("Method IV");
For (Object O:tempmap.keyset ()) {
System.out.println ("key=" + O + "value=" + tempmap.get (o));
}

System.out.println ("11111");


//Java How to traverse the 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 procedure);
}
}
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);
}
}
}
}

Related Article

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.