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

Add Stream.toList() method

XMLWordPrintable

      A DESCRIPTION OF THE REQUEST :
      `stream.collect(Collectors.toList())` and `stream.collect(Collectors.toSet())`

      are two the most popular collectors used for Stream. It would be nice to simplify/encapsulate the call by something like

      `stream.toList()` and `stream.toSet()`
      (or `stream.collectToList()` and `stream.collectToSet()`)

      JUSTIFICATION :
      It makes the coding just easier, saves some time and place.

      If you research the most of Internet posts about java streams collecting, 90% will use these collectors, thus it would be nice to have some default implementation, without specifying a collector explicitly.

      Also, `Stream#toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper)` (and related) might be helpful.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Stream interface extended to have default toList(), toSet() and optionally toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper) methods
      ACTUAL -
      collector should be explicitly specified every time

      ---------- BEGIN SOURCE ----------
      //IntStream.range(1, 1000).boxed().collect(Collectors.toList())
      IntStream.range(1, 1000).boxed().toList()
      IntStream.range(1, 1000).boxed().collectToList()


      IntStream.range(1, 1000).boxed().toSet()
      IntStream.range(1, 1000).boxed().collectToSet()

      ---------- END SOURCE ----------

            smarks Stuart Marks
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            12 Start watching this issue

              Created:
              Updated:
              Resolved: