Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8072776

Improve the collecting of list/set/map collectors


    • Type: Bug
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: tbd
    • Component/s: core-libs
    • Labels:


      The parallel mutable collecting of elements of a stream to a List:


      is not very efficient and is implemented as follows:

          public static <T>
          Collector<T, ?, List<T>> toList() {
              return new CollectorImpl<>((Supplier<List<T>>) ArrayList::new, List::add,
                                         (left, right) -> { left.addAll(right); return left; },

      Specifically the merging:

                                         (left, right) -> { left.addAll(right); return left; },

      copies all elements from the right-hand list into the left-hand list.

      Similarly for maps the merging is implemented as follows:

          private static <K, V, M extends Map<K,V>>
          BinaryOperator<M> mapMerger(BinaryOperator<V> mergeFunction) {
              return (m1, m2) -> {
                  for (Map.Entry<K,V> e : m2.entrySet())
                      m1.merge(e.getKey(), e.getValue(), mergeFunction);
                  return m1;

      Both these merging implementations are very inefficient and can reduce the amount of parallel speed up due to copying of memory and garbage collection.

      Mechanisms need to be explored to reduce the cost of merging using structural sharing. The list/set/map collectors make no guarantees on the actual concrete implementation and mutability thus there is scope to use alternative implementations that may be built up via accumulation and merging and then transformed.


          Issue Links



              • Assignee:
                chegar Chris Hegarty
                psandoz Paul Sandoz (Inactive)
              • Votes:
                0 Vote for this issue
                1 Start watching this issue


                • Created: