8164189: Collectors.toSet() parallel performance improvement
authortvaleev
Thu, 18 Aug 2016 17:30:49 -0700
changeset 40415 256109602af0
parent 40414 1f4a9da41c1f
child 40416 5d91b2fd668c
8164189: Collectors.toSet() parallel performance improvement Reviewed-by: shade, psandoz
jdk/src/java.base/share/classes/java/util/stream/Collectors.java
--- a/jdk/src/java.base/share/classes/java/util/stream/Collectors.java	Fri Aug 19 01:41:54 2016 +0300
+++ b/jdk/src/java.base/share/classes/java/util/stream/Collectors.java	Thu Aug 18 17:30:49 2016 -0700
@@ -295,7 +295,13 @@
     public static <T>
     Collector<T, ?, Set<T>> toSet() {
         return new CollectorImpl<>((Supplier<Set<T>>) HashSet::new, Set::add,
-                                   (left, right) -> { left.addAll(right); return left; },
+                                   (left, right) -> {
+                                       if (left.size() < right.size()) {
+                                           right.addAll(left); return right;
+                                       } else {
+                                           left.addAll(right); return left;
+                                       }
+                                   },
                                    CH_UNORDERED_ID);
     }