Several methods of Java Traversal map sample code _java

Source: Internet
Author: User

Several methods of traversing map in Java

Map<string,string> map=new hashmap<string,string> ();  
Map.put ("username", "QQ");  
Map.put ("PassWord", "123");  
Map.put ("UserID", "1");  
Map.put ("email", "qq@qq.com");  
Map<string,string> map=new hashmap<string,string> (); 
Map.put ("username", "QQ"); 
Map.put ("PassWord", "123"); 
Map.put ("UserID", "1"); 
Map.put ("email", "qq@qq.com"); 

The first type is used for loops

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 ()); 
} 

The second type uses iterations

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 ()); 
} 

Iterating with keyset ()

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); 
} 

Iterating with EntrySet ()

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);           
}  

 

The above is the Java traversal map of the data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!

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.