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
195
votes
11 answers

Can you split a stream into two streams?

I have a data set represented by a Java 8 stream: Stream stream = ...; I can see how to filter it to get a random subset - for example Random r = new Random(); PrimitiveIterator.OfInt coin = r.ints(0, 2).iterator(); Stream heads =…
user1148758
  • 1,963
  • 2
  • 12
  • 6
186
votes
9 answers

Should I return a Collection or a Stream?

Suppose I have a method that returns a read-only view into a member list: class Team { private List players = new ArrayList<>(); // ... public List getPlayers() { return Collections.unmodifiableList(players); …
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
182
votes
1 answer

How can I get a List from some class properties with Java 8 Stream?

I have a List. I need to get a List from a property of Person. For example, I have a Person class: class Person { private String name; private String birthDate; public String getName() { return name; } public…
NCNecros
  • 2,395
  • 3
  • 16
  • 20
175
votes
8 answers

Java 8 Streams - collect vs reduce

When would you use collect() vs reduce()? Does anyone have good, concrete examples of when it's definitely better to go one way or the other? Javadoc mentions that collect() is a mutable reduction. Given that it's a mutable reduction, I assume it…
jimhooker2002
  • 1,835
  • 2
  • 12
  • 5
174
votes
8 answers

Adding two Java 8 streams, or an extra element to a stream

I can add streams or extra elements, like this: Stream stream = Stream.concat(stream1, Stream.concat(stream2, Stream.of(element)); And I can add new stuff as I go, like this: Stream stream = Stream.concat( Stream.concat( …
Marcelo Glasberg
  • 29,013
  • 23
  • 109
  • 133
174
votes
6 answers

Java 8: performance of Streams vs Collections

I'm new to Java 8. I still don't know the API in depth, but I've made a small informal benchmark to compare the performance of the new Streams API vs the good old Collections. The test consists in filtering a list of Integer, and for each even…
Mister Smith
  • 27,417
  • 21
  • 110
  • 193
172
votes
14 answers

Zipping streams using JDK8 with lambda (java.util.stream.Streams.zip)

In JDK 8 with lambda b93 there was a class java.util.stream.Streams.zip in b93 which could be used to zip streams (this is illustrated in the tutorial Exploring Java8 Lambdas. Part 1 by Dhananjay Nene). This function : Creates a lazy and sequential…
artella
  • 5,068
  • 4
  • 27
  • 35
168
votes
10 answers

Copy a stream to avoid "stream has already been operated upon or closed"

I'd like to duplicate a Java 8 stream so that I can deal with it twice. I can collect as a list and get new streams from that; // doSomething() returns a stream List thing = doSomething().collect(toList()); thing.stream()... // do…
Toby
  • 9,523
  • 8
  • 36
  • 59
166
votes
5 answers

'Optional.get()' without 'isPresent()' check

I have the following search code in Java: return getTableViewController().getMe().getColumns().stream() .filter($ -> Database.equalsColumnName($.getId(), columnId)) .findFirst() .get(); I was wishing to find column by name and return…
Dims
  • 47,675
  • 117
  • 331
  • 600
160
votes
7 answers

Difference between Java 8 streams and RxJava observables

Are Java 8 streams similar to RxJava observables? Java 8 stream definition: Classes in the new java.util.stream package provide a Stream API to support functional-style operations on streams of elements.
rahul.ramanujam
  • 5,608
  • 7
  • 34
  • 56
159
votes
10 answers

Get last element of Stream/List in a one-liner

How can I get the last element of a stream or list in the following code? Where data.careas is a List: CArea first = data.careas.stream() .filter(c -> c.bbox.orientationHorizontal).findFirst().get(); CArea last =…
skiwi
  • 66,971
  • 31
  • 131
  • 216
155
votes
9 answers

Sorting a list with stream.sorted() in Java

I'm interested in sorting a list from a stream. This is the code I'm using: list.stream() .sorted((o1, o2)->o1.getItem().getValue().compareTo(o2.getItem().getValue())) .collect(Collectors.toList()); Am I missing something? The list is not…
Ivan C
  • 1,591
  • 2
  • 8
  • 9
148
votes
8 answers

How to check if a Java 8 Stream is empty?

How can I check if a Stream is empty and throw an exception if it's not, as a non-terminal operation? Basically, I'm looking for something equivalent to the code below, but without materializing the stream in-between. In particular, the check should…
Cephalopod
  • 14,632
  • 7
  • 51
  • 70
144
votes
6 answers

How can I create a stream from an array?

Currently whenever I need to create stream from an array, I do String[] array = {"x1", "x2"}; Arrays.asList(array).stream(); Is there some direct way to create stream from an array?
adam.kubi
  • 1,753
  • 3
  • 13
  • 14
143
votes
4 answers

Java 8 functional interface with no arguments and no return value

What is the Java 8 functional interface for a method that takes nothing and returns nothing? I.e., the equivalent to the C# parameterless Action with void return type?
Miguel Gamboa
  • 8,855
  • 7
  • 47
  • 94