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

Add Iterable.stream() and Iterable.parallelStream() methods

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      Win10x64, JDK-17ea21

      A DESCRIPTION OF THE PROBLEM :
      The java.util.Collection Interface has the two Methods java.util.Collection.stream() and java.util.Collection.parallelStream. But the Iterable Interface is missing these two methods. Is ther any reason?

      For example - the stream/parallelStream() method could be added as default methods to the Iterable Interface.
      ----

      import java.util.Collection;
      import java.util.List;
      import java.util.Optional;
      import java.util.stream.Stream;
      import java.util.stream.StreamSupport;

      public class Main {

      public static void main(String[] args) {
      final List<String> list = List.of("43", "44", "42", "41");

      System.out.println("colStream: " + colStream(list).orElse("not found"));
      System.out.println("colParallelStream: " + colParallelStream(list).orElse("not found"));
      System.out.println("iterStream: " + iterStream(list).orElse("not found"));
      System.out.println("iterParallelStream: " + iterParallelStream(list).orElse("not found"));
      }

      private final static Optional<String> colStream(final Collection<String> col) {
      // from java.util.Collection
      // as return StreamSupport.stream(spliterator(), false);
      return col.stream()// from java.util.Collection
      .filter("42"::equals)//
      .findAny();
      }

      private final static Optional<String> colParallelStream(final Collection<String> col) {
      // from java.util.Collection
      // as return StreamSupport.stream(spliterator(), true);
      return col.parallelStream()//
      .filter("42"::equals)//
      .findAny();
      }

      private final static Optional<String> iterStream(final Iterable<String> iterable) {
      // Iterable is missing .stream() but why not
      return stream(iterable) //
      .filter("42"::equals)//
      .findAny();
      }

      private final static Optional<String> iterParallelStream(final Iterable<String> iterable) {
      // Iterable is missing .parallelStream() but why not
      return parallelStream(iterable) //
      .filter("42"::equals)//
      .findAny();
      }

      public static <E> Stream<E> stream(final Iterable<E> iterable)
      {
      // Iterable is missing .stream() but why not?
      return StreamSupport.stream(iterable.spliterator(), false);
      }


      public static <E> Stream<E> parallelStream(final Iterable<E> iterable)
      {
      // Iterable is missing .stream() but why not?
      return StreamSupport.stream(iterable.spliterator(), true);
      }
      }


            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: