Import java. util. Collections list;
Import java. util. ArrayList;
Import java. util. Random;
Public class TestList {
Private static Random random = new Random ();
Public static void main (String [] args ){
ArrayList <String> arraylist = new ArrayList <String> ();
Shortlist <String> shortlist = new shortlist <String> ();
For (int I = 0; I <50000; I ++ ){
String r = randomString ();
Arraylist. add (r );
Using list. add (r );
}
System. out. println ("arraylist size" + arraylist. size ());
System. out. println ("listing list size" + listing list. size ());
System. out. println ("------");
Long begin = System. currentTimeMillis ();
For (int I = 0; I <1000; I ++ ){
For (int j = 0; j <arraylist. size (); j ++ ){
String s = arraylist. get (j );
}
}
System. out. println ("arraylist get (n) time" + (System. currentTimeMillis ()-begin ));
Begin = System. currentTimeMillis ();
For (int I = 0; I <1000; I ++ ){
For (String s: arraylist ){
}
}
System. out. println ("arraylist foreach time" + (System. currentTimeMillis ()-begin ));
Begin = System. currentTimeMillis ();
For (int I = 0; I <1; I ++)
For (int j = 0; j <partition list. size (); j ++ ){
String s = listing list. get (j );
}
System. out. println ("your list get (n) time" + (System. currentTimeMillis ()-begin) * 1000 );
Begin = System. currentTimeMillis ();
For (int I = 0; I <1000; I ++)
For (String s: Rule List ){
// Configure list. get (j );
}
System. out. println ("your list foreach time" + (System. currentTimeMillis ()-begin ));
}
Protected static String randomString (){
Return Long. toString (random. nextLong (), 36 );
}
}
Arraylist size 50000
Required list size 50000
------
Arraylist get (n) time 562
Arraylists foreach time 1703
Invalid list get (n) time 1922000
Raise list foreach time 813
Conclusion faster list foreach is faster than arraylist foreach.