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

9557 questions
990
votes
22 answers

Java 8 List into Map

I want to translate a List of objects into a Map using Java 8's streams and lambdas. This is how I would write it in Java 7 and below. private Map nameMap(List choices) { final Map hashMap = new…
Tom Cammann
  • 14,873
  • 4
  • 32
  • 47
879
votes
11 answers

How to convert a Java 8 Stream to an Array?

What is the easiest/shortest way to convert a Java 8 Stream into an array?
user972946
819
votes
19 answers

What's the difference between map() and flatMap() methods in Java 8?

In Java 8, what's the difference between Stream.map() and Stream.flatMap() methods?
cassiomolin
  • 101,346
  • 24
  • 214
  • 283
591
votes
6 answers

Should I always use a parallel stream when possible?

With Java 8 and lambdas it's easy to iterate over collections as streams, and just as easy to use a parallel stream. Two examples from the docs, the second one using parallelStream: myShapesCollection.stream() .filter(e -> e.getColor() ==…
Matsemann
  • 18,825
  • 18
  • 54
  • 88
562
votes
7 answers

Find first element by predicate

I've just started playing with Java 8 lambdas and I'm trying to implement some of the things that I'm used to in functional languages. For example, most functional languages have some kind of find function that operates on sequences, or lists that…
siki
  • 7,727
  • 3
  • 25
  • 35
523
votes
32 answers

Java 8 Distinct by property

In Java 8 how can I filter a collection using the Stream API by checking the distinctness of a property of each object? For example I have a list of Person object and I want to remove people with the same name, persons.stream().distinct(); Will use…
RichK
  • 9,771
  • 5
  • 32
  • 47
502
votes
8 answers

Java 8 Iterable.forEach() vs foreach loop

Which of the following is better practice in Java 8? Java 8: joins.forEach(join -> mIrc.join(mSession, join)); Java 7: for (String join : joins) { mIrc.join(mSession, join); } I have lots of for loops that could be "simplified" with lambdas,…
nebkat
  • 7,907
  • 9
  • 36
  • 59
465
votes
15 answers

Retrieving a List from a java.util.stream.Stream in Java 8

I was playing around with Java 8 lambdas to easily filter collections. But I did not find a concise way to retrieve the result as a new list within the same statement. Here is my most concise approach so far: List sourceLongList =…
Daniel K.
  • 5,437
  • 3
  • 15
  • 20
463
votes
8 answers

Convert Iterable to Stream using Java 8 JDK

I have an interface which returns java.lang.Iterable. I would like to manipulate that result using the Java 8 Stream API. However Iterable can't "stream". Any idea how to use the Iterable as a Stream without converting it to List?
rayman
  • 18,586
  • 41
  • 135
  • 234
431
votes
22 answers

Is there a concise way to iterate over a stream with indices in Java 8?

Is there a concise way to iterate over a stream whilst having access to the index in the stream? String[] names = {"Sam","Pamela", "Dave", "Pascal", "Erik"}; List nameList; Stream indices = intRange(1,…
Graeme Moss
  • 6,955
  • 4
  • 25
  • 39
430
votes
15 answers

Custom thread pool in Java 8 parallel stream

Is it possible to specify a custom thread pool for Java 8 parallel stream? I can not find it anywhere. Imagine that I have a server application and I would like to use parallel streams. But the application is large and multi-threaded so I want to…
Lukas
  • 11,762
  • 9
  • 27
  • 32
410
votes
12 answers

How to sum a list of integers with java streams?

I want to sum a list of Integers. It works as follows, but the syntax does not feel right. Could the code be optimized? Map integers; integers.values().stream().mapToInt(i -> i).sum();
membersound
  • 66,525
  • 139
  • 452
  • 886
392
votes
12 answers

NullPointerException in Collectors.toMap with null entry values

Collectors.toMap throws a NullPointerException if one of the values is null. I don't understand this behaviour, maps can contain null pointers as value without any problems. Is there a good reason why values cannot be null for…
Jasper
  • 4,676
  • 2
  • 11
  • 23
309
votes
17 answers

How can I throw CHECKED exceptions from inside Java 8 streams?

How can I throw CHECKED exceptions from inside Java 8 streams/lambdas? In other words, I want to make code like this compile: public List getClasses() throws ClassNotFoundException { List classes = …
MarcG
  • 21,878
  • 14
  • 83
  • 95
306
votes
4 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
  • 3,587
  • 3
  • 13
  • 17
1
2 3
99 100