Java Traversal Map Collection method

Source: Internet
Author: User

package testmap;import java.util.arraylist;import java.util.hashmap;import  Java.util.iterator;import java.util.list;import java.util.map;import java.util.map.entry;import  java.util.Set;/** *  Loop through the map collection  *  *  @author  Administrator *   */public class circlemap {public static void main (String[] args)  {Map<Integer, String> tempMap = new HashMap<Integer, String> () ; Tempmap.put (1,  "Zhang San"); Tempmap.put (2,  "John Doe"); Tempmap.put (3,  "Harry");/** * jdk1.4   Traverse method One  hashmap entryset ()   Traverse  */system.out.println ("---------------------------------------- --------------"); System.out.println (""); 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 );} In/** * jdk1.5, a new feature is applied to For-each cycle  */system.out.println (""); System.out.println ("Method two:");for  (map.entry<integer, string> entry :  Tempmap.entryset ())  {string key = entry.getkey (). toString (); String value = entry.getvalue (). toString (); System.out.println ("key = "  + key +  " value = "  + value );} /** *  Traversal Method Three  hashmap keyset ()   Traverse  */system.out.println (""); System.out.println ("Method Three:");for  (Iterator i = tempmap.keyset (). Iterator ();  i.hasnext () ;)  {object obj = i.next (); System.out.println (obj);//  loop output KeySystem.out.println ("key = "  + obj +  "  value =  " + tempmap.get (obj));} For&nbSP; (Iterator i = tempmap.values (). Iterator ();  i.hasnext ();)  {object obj =  i.next (); System.out.println (obj);//  loop output value}/** *  Traversal method four  treemap keyset () traversal  */ System.out.println (""); System.out.println ("Method Four");for  (Object o : tempmap.keyset ())  {system.out.println ("Key  =  " + o + "  value =  " + tempmap.get (o));} System.out.println ("------------------------------------------------------");/** * java how to traverse map  <String, ArrayList> map = new HashMap * <String,ArrayList> (); &NBSP;*//*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>&nBsp;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 ("======================================="); 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);}} */}}

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/82/7B/wKioL1dWhfXyQbbjAAA43J61sOY922.png-wh_500x0-wm_3 -wmp_4-s_1549625600.png "title=" Test Through "alt=" Wkiol1dwhfxyqbbjaaa43j61soy922.png-wh_50 "/>

This article is from the "Phpajax request" blog, so be sure to keep this source http://15129824665.blog.51cto.com/10510385/1787011

Java Traversal Map Collection 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.