Test environment: JDK1.8.0_60, Guava-18.0.jar
Tomap
public static void Demo1 () {int size = 50000;
list<string> list = new arraylist<> (size);
for (int i = 0; i < size; i++) {List.add (integer.tostring (i));
} System.out.println ("size =" + size);
Long Start2 = System.currenttimemillis ();
Map<integer, string> map3 = maps.newhashmapwithexpectedsize (size); for (int i = 0; i < size; i++) {//String string = List.get (i);//Map3.put (String.has
Hcode (), string);
for (string string:list) {Map3.put (String.hashcode (), string);
Long End2 = System.currenttimemillis ();
System.out.println ("iterator---t =" + (END2-START2));
Long start = System.currenttimemillis (); Map<integer, string> map = List.parallelstream (). Collect (Collectors.tomap (S-> s.hashcode (), S-
> s));
Long end = System.currenttimemillis (); SYSTEM.OUT.PRINTLN ("Stream---t =" + (End-start));
Long Start1 = System.currenttimemillis (); Map<integer, string> map1 = Maps.uniqueindex (list,//lists for value list new Com.google.common.base.Functi On<string, integer> () {@Override public Integer apply (String value) {//return value is
Key, the parameter is Value return Value.hashcode ();
}
});
Long end1 = System.currenttimemillis ();
SYSTEM.OUT.PRINTLN ("Guava---t =" + (END1-START1)); }
Note the map type generated by guava.
Test results:
Size = 5000
iterator---t =
---t =
guava---t =
size = 500000
iterator---t = 148< C5/>stream---t = 1533
Guava---t = 104
Filter
public static void Demo2 () {int size = 5000;
list<string> list = new arraylist<> (size);
for (int i = 0; i < size; i++) {List.add (integer.tostring (i));
} System.out.println ("Szie =" + size);
Long Start2 = System.currenttimemillis ();
List<string> List2 = lists.newarraylistwithcapacity (size);
for (int i = 0; i < size; i++) {String string = List.get (i);
if (integer.valueof (string)% 2 = 0) {list2.add (string);
} Long End2 = System.currenttimemillis ();
System.out.println ("iterator---t =" + (END2-START2));
Long start = System.currenttimemillis (); List<string> List1 = List.parallelstream (). Filter (S-> integer.valueof (s)% 2 = 0). Collect (Coll
Ectors.tolist ());
Long end = System.currenttimemillis (); SYSTEM.OUT.PRINTLN ("Stream---t =" + (end-sTart)); }
Test results:
Szie = 5000
iterator---t =
---t =
Szie = 500000
iterator---t =
stream---t = 1 33
AnyMatch
public static void Demo3 () {
int size = 500000;
list<string> list = new arraylist<> (size);
for (int i = 0; i < size; i++) {
List.add (integer.tostring (i));
}
System.out.println ("Szie =" + size);
Long Start2 = System.currenttimemillis ();
for (int i = 0; i < size; i++) {
string string = List.get (i);
if (String.endswith ("412347")) {break
;
}
}
Long End2 = System.currenttimemillis ();
System.out.println ("iterator---t =" + (END2-START2));
Long start = System.currenttimemillis ();
List.parallelstream (). AnyMatch (S-> s.endswith ("1234567"));
Long end = System.currenttimemillis ();
SYSTEM.OUT.PRINTLN ("Stream---t =" + (End-start));
}
Test results:
Szie = 500000
iterator---t =
stream---t = 114