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
1049
votes
10 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
1042
votes
23 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
  • 15,798
  • 4
  • 37
  • 48
961
votes
22 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
  • 124,154
  • 35
  • 280
  • 359
682
votes
8 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
  • 9,077
  • 3
  • 27
  • 36
656
votes
34 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
  • 11,318
  • 6
  • 35
  • 49
648
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
  • 21,083
  • 19
  • 56
  • 89
548
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
  • 8,445
  • 9
  • 41
  • 60
530
votes
13 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
  • 6,076
  • 2
  • 14
  • 24
511
votes
26 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
  • 7,995
  • 4
  • 29
  • 42
509
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
  • 20,786
  • 45
  • 148
  • 246
493
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,747
  • 3
  • 19
  • 22
480
votes
16 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
  • 13,606
  • 9
  • 31
  • 40
478
votes
13 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
  • 81,582
  • 193
  • 585
  • 1,120
387
votes
12 answers

Ignore duplicates when producing map using streams

Map phoneBook = people.stream() .collect(toMap(Person::getName, Person::getAddress)); I get java.lang.IllegalStateException: Duplicate key…
Patan
  • 17,073
  • 36
  • 124
  • 198
365
votes
18 answers

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

How can I throw CHECKED exceptions from inside Java 8 lambda, used in a stream for example? In other words, I want to make code like this compile: public List getClasses() throws ClassNotFoundException { List classes = …
Marcelo Glasberg
  • 29,013
  • 23
  • 109
  • 133
1
2 3
99 100