Methods for looping map and obtaining map values in java

Source: Internet
Author: User
Tags set set

Cyclic traversal of map

The code is as follows: Copy code
Package com. sec. map;
   
Import java. util. HashMap;
Import java. util. Iterator;
Import java. util. Map;
   
Public class TestMap {
   
Public static void main (String [] args ){
   
   
Map <String, String> map = new HashMap <String, String> ();
 
Map. put ("1", "value1 ");
 
Map. put ("2", "value2 ");
 
Map. put ("3", "value3 ");
 
             
// The first type is commonly used. The second value is used.
 
System. out. println ("traverse key and value through Map. keySet :");
 
For (String key: map. keySet ()){
 
System. out. println ("key =" + key + "and value =" + map. get (key ));
 
          }
 
System. out. println ("www.111cn.net ");
 
// Type 2
 
System. out. println ("traverse key and value through Map. entrySet using iterator :");
 
Iterator <Map. Entry <String, String> it = map. entrySet (). iterator ();
 
While (it. hasNext ()){
 
Map. Entry <String, String> entry = it. next ();
 
System. out. println ("key =" + entry. getKey () + "and value =" + entry. getValue ());
 
          }
 
System. out. println ("www.111cn.net ");
 
// Method 3: recommended, especially when the capacity is large
 
System. out. println ("traversing key and value through Map. entrySet ");
 
For (Map. Entry <String, String> entry: map. entrySet ()){
 
System. out. println ("key =" + entry. getKey () + "and value =" + entry. getValue ());
 
          }
 
System. out. println ("www.111cn.net ");
 
// Category 4
 
System. out. println ("traverse all values through Map. values (), but not key ");
 
For (String v: map. values ()){
 
System. out. println ("value =" + v );
 
          }
 
System. out. println ("www.111cn.net ");
         }
 
}

Supplement:

First, use the for loop
Java code

The code is as follows: Copy code

For (Map. Entry <String, String> entry: map. entrySet ()){
System. out. println (entry. getKey () + "--->" + entry. getValue ());
}  
For (Map. Entry <String, String> entry: map. entrySet ()){
System. out. println (entry. getKey () + "--->" + entry. getValue ());
}

Second, iteration
Java code

The code is as follows: Copy code
Set set = map. entrySet ();
Iterator I = set. iterator ();
While (I. hasNext ()){
Map. Entry <String, String> entry1 = (Map. Entry <String, String>) I. next ();
System. out. println (entry1.getKey () + "=" + entry1.getValue ());
}  
Set set = map. entrySet ();
Iterator I = set. iterator ();
While (I. hasNext ()){
Map. Entry <String, String> entry1 = (Map. Entry <String, String>) I. next ();
System. out. println (entry1.getKey () + "=" + entry1.getValue ());
}

Use keySet () for iteration
Java code

The code is as follows: Copy code

Iterator it = map. keySet (). iterator ();
While (it. hasNext ()){
String key;
String value;
Key = it. next (). toString ();
Value = map. get (key );
System. out. println (key + "--" + value );
}  
Iterator it = map. keySet (). iterator ();
While (it. hasNext ()){
String key;
String value;
Key = it. next (). toString ();
Value = map. get (key );
System. out. println (key + "--" + value );
}

Use entrySet () for iteration
Java code

The code is as follows: Copy code

Iterator it = map. entrySet (). iterator ();
System. out. println (map. entrySet (). size ());
String key;
String value;
While (it. hasNext ()){
Map. Entry entry = (Map. Entry) it. next ();
Key = entry. getKey (). toString ();
Value = entry. getValue (). toString ();
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.