Java streams 14. Stream factory methods and operations

  static Stream<T> generate(Supplier<T> s);
static Stream<T> iterate(T seed, UnaryOperator<T> f);
static Stream<T> iterate(T seed, Predicate<T> hasNext,
UnaryOperator<T> next);
static Stream.Builder<T> builder();
static Stream<T> of(T... values);
static Stream<T> empty();
static Stream<T> ofNullable(T t);
static Stream<T> concat(Stream<T> a, Stream<T> b);
  Stream<T> sorted();
Stream<T> distinct();
Stream<T> skip(long n);
Stream<T> limit(long maxSize);
Stream<T> filter(Predicate<T> predicate);
Stream<T> sorted(Comparator<T> comparator);
default Stream<T> dropWhile(Predicate<T> predicate);
default Stream<T> takeWhile(Predicate<T> predicate);
  Stream<R> map(Function<T,R> mapper);
IntStream mapToInt(ToIntFunction<T> mapper);
LongStream mapToLong(ToLongFunction<T> mapper);
DoubleStream mapToDouble(ToDoubleFunction<T> mapper);
  Stream<R> flatMap(Function<T, Stream<R>> mapper);
IntStream flatMapToInt(Function<T,IntStream> mapper);
LongStream flatMapToLong(Function<T,LongStream> mapper);
DoubleStream flatMapToDouble(Function<T,DoubleStream> mapper);
Stream<T> peek(Consumer<T> action);
    List.of("1","2","3").stream()
.map(s -> s + "a")
.peek(System.out::println)
.map(s -> s + "b")
.forEach(System.out::println);
  1a
1ab
2a
2ab
3a
3ab
  long count();
void forEach(Consumer<T> action);
void forEachOrdered(Consumer<T> action);
boolean allMatch(Predicate<T> predicate);
boolean anyMatch(Predicate<T> predicate);
boolean noneMatch(Predicate<T> predicate);
Optional<T> findAny();
Optional<T> findFirst();
Optional<T> max(Comparator<T> comparator);
Optional<T> min(Comparator<T> comparator);
Object[] toArray();
A[] toArray(IntFunction<A[]> generator);
Optional<T> reduce(BinaryOperator<T> accumulator);
T reduce(T identity, BinaryOperator<T> accumulator);
U reduce(U identity, BiFunction<U,T,U> accumulator,
BinaryOperator<U> combiner);
R collect(Collector<T,A,R> collector);
R collect(Supplier<R> supplier, BiConsumer<R,T> accumulator,
BiConsumer<R,R> combiner);
  long c = List.of("a","b","c").stream().count();
System.out.print(c); //prints: 3
    List.of("1","2","3","4")
.parallelStream()
.forEach(System.out::print); //prints: 3421
List.of("1","2","3","4")
.parallelStream()
.forEachOrdered(System.out::print); //prints: 1234

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nick Samoylov

Nick Samoylov

Born in Moscow, lived in Crimea, now lives in the US. Used to be physicist and rock climber, now programmer and writer.