public static void Compare () {
list<string> list =New Arraylist<string> ();
Set<string> set =New Hashset<string> ();
Forint i=0;i<100000;i++) {
List.add ("A" +i);
}
Long t1=0l,t2=0L;
T1 = System.Currenttimemillis ();
for (String s:list) {
Set.add (s);
}
t2 = System.Currenttimemillis ();
System.Out.println (The time for the foreach traversal is: "+ (T2-T1) +"MM");
Both
T1 = System.Currenttimemillis ();
Forint i=0; I<list.size (); i++) {
String str = list.get (i);
Set.add (List.get (i));
}
t2 = System.Currenttimemillis ();
System.Out.println ( ////three
T1 = System. currenttimemillis ();
Iterator<string> Iterator = List.iterator ();
while (Iterator.hasnext ()) {
String str = Iterator.next ();
Set.add (String) Iterator.next ());
}
t2 = System. Currenttimemillis ();
System. out.println (" iterator traversal time: "+ (T2-T1) +" MM ");
}
foreach traversal is: 45mm
Size traversal time: 14mm
Iterator traversal: 7mm
Java list Three traversal method performance comparison