Summary of Java list loop and map loop
Do a list and map of the summary, there is no technical content, all the review of the API.
The test environment is under JUNIT4, and it is the same without writing a main method yourself.
First, there are three loops of the list:
@Test public
void Forlisttest () {
list<string> List = new arraylist<string> ();
List.add ("1");
List.add ("2");
List.add ("3");
List.add ("4");
List.add ("5");
Iterator loops do not need to know the size and type of the collection, the best choice for
(@SuppressWarnings ("Rawtypes")
iterator iterator = List.iterator (); Iterator.hasnext ();) {
String list = (string) iterator.next ();
System.out.println (") iterator for:===============" + list);
}
foreach is not required to know the length of the collection for
(String list:list) {
System.out.println ("foreach:=================") relative to the for. + list);
}
A For loop needs to know the size of the collection and requires an ordered for
(int i = 0; i < list.size (); i++) {
System.out.println () for============= ===== "+ list.get (i));
}
}
Then there are four loops of map:
@Test public void Formaptest () {map<string, string> Map = new hashmap<string, string> ();
Map.put ("01", "1");
Map.put ("02", "2");
Map.put ("03", "3");
Map.put ("04", "4");
Map.put ("05", "5");
Set<string> keyset = Map.keyset ();
1.keyset foreach method for (String key:keyset) {System.out.println ("1) Keyset:" + "key:" + key + "value:"
+ map.get (key));
} set<entry<string, string>> entryset = Map.entryset ();
2.entryset iterative method for (@SuppressWarnings ("Rawtypes") Iterator iterator = Entryset.iterator (); Iterator.hasnext ();) {
@SuppressWarnings ("Unchecked") entry<string, string> Entry = (entry<string, string>) iterator
. Next ();
System.out.println (") Entryset,iterator:key:" + entry.getkey () + "value:" + entry.getvalue ()); //3. Recommended, capacity Max for (entry<string, string> entry:entryset) {System.out.println (") Entryset,foreach:ke Y: "+ entry.geTKey () + "value:" + entry.getvalue ());
} collection<string> values = Map.values ();
4. The method for only looping out the value for (String value:values) {System.out.println (") Values,just for Values,value:" + value);
}
}
Thank you for reading, I hope to help you, thank you for your support for this site!