Questions tagged [java-stream]

Use this tag for questions related to the use of the Stream API. It was introduced in Java 8 and supports functional-style operations on streams of values, such as filter-map-reduce pipelines on collections.

The Streams API is an API introduced in Java 8 that supports functional-style operations on streams of values, such as filter-map-reduce pipelines on collections. A primary design goal of the Streams API is to support parallel processing.

Features

  • Mapping and flat-mapping
  • Filtering
  • Reducing and aggregating such as count, sum or average
  • Collecting and grouping to Map, List and Set.
  • Summarizing

References

11473 questions
361
votes
3 answers

Java 8 lambdas, Function.identity() or t->t

I have a question regarding the usage of the Function.identity() method. Imagine the following code: Arrays.asList("a", "b", "c") .stream() .map(Function.identity()) // <- This, .map(str -> str) // <- is the…
user4464654
340
votes
5 answers

What is difference between Collection.stream().forEach() and Collection.forEach()?

I understand that with .stream(), I can use chain operations like .filter() or use parallel stream. But what is difference between them if I need to execute small operations (for example, printing the elements of the…
VladS
  • 4,116
  • 4
  • 16
  • 17
327
votes
4 answers

Java 8 Streams: multiple filters vs. complex condition

Sometimes you want to filter a Stream with more than one condition: myList.stream().filter(x -> x.size() > 10).filter(x -> x.isCool()) ... or you could do the same with a complex condition and a single filter: myList.stream().filter(x -> x.size() >…
deamon
  • 89,107
  • 111
  • 320
  • 448
327
votes
24 answers

Filter Java Stream to 1 and only 1 element

I am trying to use Java 8 Streams to find elements in a LinkedList. I want to guarantee, however, that there is one and only one match to the filter criteria. Take this code: public static void main(String[] args) { LinkedList users = new…
ryvantage
  • 13,064
  • 15
  • 63
  • 112
306
votes
12 answers

Using Java 8's Optional with Stream::flatMap

The new Java 8 stream framework and friends make for some very concise Java code, but I have come across a seemingly-simple situation that is tricky to do concisely. Consider a List things and method Optional resolve(Thing thing). I…
Yona Appletree
  • 8,801
  • 6
  • 35
  • 52
306
votes
9 answers

Why does Stream not implement Iterable?

In Java 8 we have the class Stream, which curiously have a method Iterator iterator() So you would expect it to implement interface Iterable, which requires exactly this method, but that's not the case. When I want to iterate over a Stream…
roim
  • 4,780
  • 2
  • 27
  • 35
304
votes
3 answers

Java 8 method references: provide a Supplier capable of supplying a parameterized result

I'd like to use java.util.Optional.orElseThrow() with an Exception type that asks for a constructor parameter. Something like this: .orElseThrow(MyException::new(someArgument)) // obviously NOT working Is there a way to create a Supplier that…
mdo
  • 6,961
  • 3
  • 24
  • 26
288
votes
20 answers

How to convert List to Map?

Recently I have conversation with a colleague about what would be the optimal way to convert List to Map in Java and if there any specific benefits of doing so. I want to know optimal conversion approach and would really appreciate if any one can…
Rachel
  • 100,387
  • 116
  • 269
  • 365
280
votes
10 answers

Java8: HashMap to HashMap using Stream / Map-Reduce / Collector

I know how to "transform" a simple Java List from Y -> Z, i.e.: List x; List y = x.stream() .map(s -> Integer.parseInt(s)) .collect(Collectors.toList()); Now I'd like to do basically the same with a Map,…
Benjamin M
  • 23,599
  • 32
  • 121
  • 201
274
votes
15 answers

Using streams to convert a list of objects into a string obtained from the toString method

There are a lot of useful new things in Java 8. E.g., I can iterate with a stream over a list of objects and then sum the values from a specific field of the Object's instances. E.g. public class AClass { private int value; public int getValue()…
mat_boy
  • 12,998
  • 22
  • 72
  • 116
271
votes
3 answers

Why does Iterable not provide stream() and parallelStream() methods?

I am wondering why the Iterable interface does not provide the stream() and parallelStream() methods. Consider the following class: public class Hand implements Iterable { private final List list = new ArrayList<>(); private…
skiwi
  • 66,971
  • 31
  • 131
  • 216
264
votes
13 answers

How to use a Java8 lambda to sort a stream in reverse order?

I'm using java lambda to sort a list. how can I sort it in a reverse way? I saw this post, but I want to use java 8 lambda. Here is my code (I used * -1) as a hack Arrays.asList(files).stream() .filter(file -> isNameLikeBaseLine(file,…
Elad Benda2
  • 13,852
  • 29
  • 82
  • 157
251
votes
8 answers

Java Stream API - Best way to transform a list: map or forEach?

I have a list, myListToParse, where I want to filter the elements and apply a method on each element, and add the result in another list, myFinalList. With the Stream API (added in Java 8), I noticed that I can do it in 2 different ways. I would…
Emilien Brigand
  • 9,943
  • 8
  • 32
  • 37
248
votes
6 answers

Why are Java Streams once-off?

Unlike C#'s IEnumerable, where an execution pipeline can be executed as many times as we want, in Java a stream can be 'iterated' only once. Any call to a terminal operation closes the stream, rendering it unusable. This 'feature' takes away a lot…
Vitaliy
  • 8,044
  • 7
  • 38
  • 66
232
votes
7 answers

Java 8: How do I work with exception throwing methods in streams?

Suppose I have a class and a method class A { void foo() throws Exception() { ... } } Now I would like to call foo for each instance of A delivered by a stream like: void bar() throws Exception { Stream as = ... as.forEach(a ->…
Bastian
  • 4,638
  • 6
  • 36
  • 55