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!