First, take a look at the map and put some values in a map, the code is as follows:
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");
First: Traverse key and value via Map.keyset
For (String Key:map.keySet ()) {
String Value=map.get (key);
System.out.println ("key==" +key+ "and value=" +value);
}
The second type: Use iterator to traverse key and value via Map.entryset
Iterator<map.entry<string, String>> Itr=map.entryset (). Iterator ();
while (Itr.hasnext ()) {
Map.entry<string, string> entry=itr.next ();
String Key=entry.getkey ();
String Value=entry.getvalue ();
System.out.println ("key=" +key+ "value=" +value);
}
The third type: Traverse key and value via Map.entryset
For (map.entry<string, string> entry:map.entrySet ()) {
String Key=entry.getkey ();
String Value=entry.getvalue ();
System.out.println ("key=" +key+ "-------->value=" +value);
}
Fourth: Direct traversal of value
For (String s:map.values ()) {
System.out.println ("value=" +s);
}
The results of the implementation are as follows:
Key==3 and Value=value3
Key==2 and Value=value2
Key==1 and Value=value1
Key=3 Value=value3
key=2 value=value2
Key=1 value=value1
Key=3-------->value=value3
key=2-------->value=value2
Key=1-------->value=value1
Value=value3
Value=value2
Value=value1
Let's talk about the list traversal method:
This method inside my list is the user object, first built an object code as follows:
Package com.cn.domain;
public class Gost {
Private long ID;
private String name;
private String password;
Public long getId () {
return ID;
}
public void SetId (long id) {
This.id = ID;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public String GetPassword () {
return password;
}
public void SetPassword (String password) {
This.password = password;
}
}
Next write a class to test, first put some data in the list, the code is as follows:
Package test;
Import java.util.ArrayList;
Import Java.util.Iterator;
Import java.util.List;
Import Com.cn.domain.Gost;
public class Testlist {
public static void Main (String args[]) {
List<gost> gostlist=new arraylist<gost> ();
GOST gost1=new gost ();
Gost1.setid (1);
Gost1.setname ("Z3");
Gost1.setpassword ("111");
Gostlist.add (GOST1);
GOST gost2=new gost ();
Gost2.setid (2);
Gost2.setname ("L4");
Gost2.setpassword ("222");
Gostlist.add (GOST2);
GOST gost3=new gost ();
Gost3.setid (3);
Gost3.setname ("W5");
Gost3.setpassword ("333");
Gostlist.add (GOST3);
Then there are three ways to traverse, the code is as follows:
Method 1: Strengthen the For loop
for (Gost s:gostlist) {
System.out.println (S.getid () + "--" +s.getname () + "--" +s.getpassword ());
}
Method 2:for Loop
for (int i=0;i<gostlist.size (); i++) {
Gost Gost=gostlist.get (i);
System.out.println (Gost.getid () + "-" +gost.getname () + "-" +gost.getpassword ());
}
Method 3,iterator
Iterator It=gostlist.iterator ();
while (It.hasnext ()) {
GOST gost= (GOST) it.next ();
System.out.println (Gost.getid () + "-" +gost.getname () + "-" +gost.getpassword ());
}
The result of executing the program is:
1--z3--111
2--l4--222
3--w5--333
1-z3-111
2-l4-222
3-w5-333
1-z3-111
2-l4-222
3-w5-333
About the traversal method of list and map