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
228
votes
31 answers

Java 8 stream reverse order

General question: What's the proper way to reverse a stream? Assuming that we don't know what type of elements that stream consists of, what's the generic way to reverse any stream? Specific question: IntStream provides range method to generate…
vach
  • 10,571
  • 12
  • 68
  • 106
223
votes
4 answers

Why is a combiner needed for reduce method that converts type in java 8

I'm having trouble fully understanding the role that the combiner fulfills in Streams reduce method. For example, the following code doesn't compile: int length = asList("str1", "str2").stream() .reduce(0, (accumulatedInt, str) ->…
Louise Miller
  • 3,069
  • 2
  • 12
  • 13
223
votes
5 answers

Adding up BigDecimals using Streams

I have a collection of BigDecimals (in this example, a LinkedList) that I would like to add together. Is it possible to use streams to calculate their sum? I noticed the Stream class has several…
ryvantage
  • 13,064
  • 15
  • 63
  • 112
219
votes
19 answers

Limit a stream by a predicate

Is there a Java 8 stream operation that limits a (potentially infinite) Stream until the first element fails to match a predicate? In Java 9 we can use takeWhile as in the example below to print all the numbers less than 10. IntStream …
MForster
  • 8,806
  • 5
  • 28
  • 32
218
votes
10 answers

Does Java SE 8 have Pairs or Tuples?

I am playing around with lazy functional operations in Java SE 8, and I want to map an index i to a pair / tuple (i, value[i]), then filter based on the second value[i] element, and finally output just the indices. Must I still suffer this: What is…
necromancer
  • 23,916
  • 22
  • 68
  • 115
218
votes
5 answers

Java 8 stream's .min() and .max(): why does this compile?

Note: this question originates from a dead link which was a previous SO question, but here goes... See this code (note: I do know that this code won't "work" and that Integer::compare should be used -- I just extracted it from the linked…
fge
  • 119,121
  • 33
  • 254
  • 329
216
votes
5 answers

In Java, what are the advantages of streams over loops?

I was asked this at an interview and I'm not convinced I gave the best answer I could have. I mentioned that you can do a parallel search and that null values were handled by some means I couldn't remember. Now I realize I was thinking of Optionals.…
user447607
  • 5,149
  • 13
  • 33
  • 55
215
votes
4 answers

What Java 8 Stream.collect equivalents are available in the standard Kotlin library?

In Java 8, there is Stream.collect which allows aggregations on collections. In Kotlin, this does not exist in the same way, other than maybe as a collection of extension functions in the stdlib. But it isn't clear what the equivalences are for…
Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
215
votes
4 answers

Java 8 Stream and operation on arrays

I have just discovered the new Java 8 stream capabilities. Coming from Python, I was wondering if there was now a neat way to do operations on arrays like summing, multiplying two arrays in a "one line pythonic" way ? Thanks
BlackLabrador
  • 2,829
  • 5
  • 18
  • 21
214
votes
10 answers

In Java streams is peek really only for debugging?

I'm reading up about Java streams and discovering new things as I go along. One of the new things I found was the peek() function. Almost everything I've read on peek says it should be used to debug your Streams. What if I had a Stream where each…
Adam.J
  • 2,519
  • 3
  • 14
  • 12
212
votes
8 answers

How to add elements of a Java8 stream into an existing List

Javadoc of Collector shows how to collect elements of a stream into a new List. Is there an one-liner that adds the results into an existing ArrayList?
codefx
  • 9,872
  • 16
  • 53
  • 81
210
votes
5 answers

Is it possible to cast a Stream in Java 8?

Is it possible to cast a stream in Java 8? Say I have a list of objects, I can do something like this to filter out all the additional objects: Stream.of(objects).filter(c -> c instanceof Client) After this though, if I want to do something with…
Phiction
  • 2,387
  • 2
  • 12
  • 7
202
votes
8 answers

In Java 8 how do I transform a Map to another Map using a lambda?

I've just started looking at Java 8 and to try out lambdas I thought I'd try to rewrite a very simple thing I wrote recently. I need to turn a Map of String to Column into another Map of String to Column where the Column in the new Map is a…
annesadleir
  • 2,141
  • 2
  • 12
  • 8
201
votes
2 answers

How to ensure order of processing in java8 streams?

I want to process lists inside an XML java object. I have to ensure processing all elements in order I received them. Should I therefore call sequential on each stream I use? list.stream().sequential().filter().forEach() Or it it sufficient to just…
membersound
  • 81,582
  • 193
  • 585
  • 1,120
199
votes
3 answers

Fetch first element of stream matching the criteria

How to get first element that matches a criteria in a stream? I've tried this but doesn't work this.stops.stream().filter(Stop s-> s.getStation().getName().equals(name)); That criteria is not working, the filter method is invoked in an other class…
user2147674
  • 2,319
  • 2
  • 14
  • 17