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
7
votes
5 answers

Is it possible to write a Java Collector that does early exit when it has a result?

Is it possible to implement a Collector that stops processing of the stream as soon as an answer is available? For example, if the Collector is computing an average, and one of the values is NaN, I know the answer is going to be NaN without seeing…
Michael Kay
  • 156,231
  • 11
  • 92
  • 164
7
votes
1 answer

Two level filtering of list in Java 8

I have a list aList of objects of class A. This aList is member of each element b of another list bList. Each element b is of class B. Structure of class B is as follows: class B { String status; String name; } Structure of class A is as…
Joy
  • 4,197
  • 14
  • 61
  • 131
7
votes
4 answers

Group by for a list of int arrays

I have a list of int arrays. I want to group that by unique arrays. int[] array1 = new int[]{1, 2, 3}; int[] array2 = new int[]{1, 2, 3}; //array1 = array2 int[] array3 = new int[]{0, 2, 3}; List test = new…
Aleksandr
  • 83
  • 1
  • 4
7
votes
1 answer

Stream GroupingBy for an Object Inside another Different Object

I have three Objects as below : public class A { private int a; private ... private B b; //getters setters for B } public class B { private String name; private ... private C c; //getters setters for…
iamP
  • 307
  • 1
  • 3
  • 13
7
votes
3 answers

Java stream - collect combiner

Why does the following code: StringBuilder sb22 = IntStream .range(1, 101) .filter(x -> x > 50) .boxed() .parallel() .collect(// object that is used in accumulator to do accumulating on StringBuilder::new,…
Bojan Vukasovic
  • 2,054
  • 22
  • 43
7
votes
3 answers

XXXSummaryStatistics new constructor in java-10

I see that java-10 adds a constructor for IntSummaryStatistics (LongSummaryStatistics and DoubleSummaryStatistics) that takes 4 parameters that are count, min, max and sum. I understand why the no-args constructor exists, so that it would be used in…
Eugene
  • 117,005
  • 15
  • 201
  • 306
7
votes
2 answers

Java 8 Stream get minimum

I have to write a method with the stream api.In the method I should get the minimum of an integer value in an Object called "Winner", The Integer value I mean is the lengthKm value, I have to get the lowest/shortest one.The Class looks like this…
user9064113
7
votes
2 answers

How to throw a custom exception from CompletableFuture?

Question: how can I directly throw a custom exception from .exceptionally()? List> futures = tasks.stream() .map(task -> CompletableFuture.supplyAsync(() -> businessLogic(task)) .exceptionally(ex -> { …
membersound
  • 81,582
  • 193
  • 585
  • 1,120
7
votes
2 answers

Java API Streams collecting stream in Map where value is a TreeSet

There is a Student class which has name, surname, age fields and getters for them. Given a stream of Student objects. How to invoke a collect method such that it will return Map where keys are age of Student and values are TreeSet which contain…
False Promise
  • 478
  • 3
  • 6
  • 13
7
votes
3 answers

How to sort object using java 8 streams

I am using the stream API in java 8 to handle my collections. However, I am wondering what would be an elegant way to sort my objects in a given order, using this API. SortedCollection = inputCollection.stream() .map(e -> { return new…
E. Bavoux
  • 535
  • 1
  • 4
  • 11
7
votes
2 answers

Java 8 Lambda (grouping and reducing in a single step)

Say I have a List of Pair objects, List> listOfPairs = //some list of pairs ; I would like to group this list into a Map>. Currently, I can do this in two steps. The first step groups by A, and returns a Map>…
mancini0
  • 4,285
  • 1
  • 29
  • 31
7
votes
3 answers

Java - Create an IntStream with a given range, then randomise each element using a map function

So I've created an IntStream where I give it a range of 1 - 9. I would like to be able to use the map function to take each element in the given range (1-9) and randomise each one. Essentially I would like to stream the numbers 1 - 9 in a different…
SneakyShrike
  • 723
  • 1
  • 10
  • 31
7
votes
1 answer

Java Stream API: Looking for elegant way for filterAndMap

The default 'best practice' to filter and map a stream is Stream source; // ... Predicate predicate; // = ... Function mapper; // = ... Stream dst = source .filter(predicate) .map(mapper); In many software projects…
Torsten Fehre
  • 567
  • 2
  • 7
7
votes
2 answers

Java 8 Parallel Stream Concurrent Grouping

Suppose I have a class as Class Person { String name; String uid; String phone; } I am trying to group by all the fields of the class. How do i use parallel streams in JAVA 8 to convert a List into Map> where the…
user3665053
  • 83
  • 1
  • 4
7
votes
4 answers

Java 8: Stream a list and map to another list based on different filters

I have the following code: public boolean foo(List source, String bar, String baz) { List myList = newArrayList(); source.forEach(json -> { if (!(json.get(bar) instanceof JSONObject)) { …
iamkenos
  • 1,446
  • 8
  • 24
  • 49