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
2 answers

Java stream: use optional filter() operations on chaining

Note: this question is not related to java.util.Optional. When dealing with streams, I often use logic like this: Stream stream = myInitialStream(); if (needsFilter1) stream = stream.filter(c -> whatever1()); if (needsFilter2) stream =…
Alkis Mavridis
  • 1,090
  • 12
  • 28
7
votes
3 answers

Does Collection.parallelStream() imply a happens-before relationship?

Consider this (completely contrived) Java code: final List s = Arrays.asList(1, 2, 3); final int[] a = new int[1]; a[0] = 100; s.parallelStream().forEach(i -> { synchronized (a) { a[0] += i; …
Archie
  • 4,959
  • 1
  • 30
  • 36
7
votes
2 answers

How do I create two different compliementary lists using same input

In my previous question - How to filter the age while grouping in map with list I was able to find the name to age groups using List users. Now I am trying to find the different User groups from ages according to the threshold. I tried this …
Mani
  • 1,068
  • 3
  • 13
  • 27
7
votes
3 answers

Using Lambda functions to Consume all objects provided by a Supplier

Looking for how to use Java lambda functions so Consumer can process all objects provided by a Supplier, and get rid of the explicit while loop and null checks. I have a Supplier of String keys for a database and I want to use a Consumer to process…
ScottK
  • 1,526
  • 1
  • 16
  • 23
7
votes
3 answers

Why does .forEach(val -> list.add()) compile whereas .forEach(val -> true) doesn't?

It's better to express this behavior in the code: List list= new ArrayList<>(); Stream.of(1,2,3).forEach(i -> list.add(1)); // COMPILES Stream.of(1,2,3).forEach(i -> true); // DOES NOT COMPILE! forEach(...) accepts Consumer, but why…
Leonid Dashko
  • 3,657
  • 1
  • 18
  • 26
7
votes
1 answer

creating nested maps with nested properties from List, stream, Java 8

I made this question stream creating List of List (nested List) using forEach, Java 8 class EntityCompositeId { private Long firstId; private Long secondId; // getter & setter... } class EntityComposite { private EntityCompositeId…
joseluisbz
  • 1,491
  • 1
  • 36
  • 58
7
votes
2 answers

Find most recent date in a list of objects on LocalDate property using Java 8 stream

I have a list of objects which hold multiple properties, one of which is a LocalDate. I'd like to find the object with the most recent date out of this list. I'm fairly green with Java 8 and using streams. Like most programming, it seems as though…
acousticarmor
  • 81
  • 1
  • 1
  • 6
7
votes
3 answers

Java Stream sum() short circuiting

While doing a project I wrote this line, basically it decided whether or not to merge the current node based on how many children there are. int succNodes = Arrays.stream(children).mapToInt(PRQuadNode::count).sum(); if (succNodes <= bucketingParam)…
Chris
  • 145
  • 8
7
votes
4 answers

Good way to filter list distincted by property and ordered by date

I have very simple thing to do, i have list of persons like this: [{ name: John, date: 01-01-2018, attend: true }, { name: Adam, date: 01-01-2018, attend: false }, { name: Adam, date: 01-02-2018, attend:…
Sahbaz
  • 1,242
  • 4
  • 17
  • 39
7
votes
2 answers

Apply reduction only if certain condition is met

Is there a way to let the 'reduction' of the reduce() method of Stream be optional? I want to iterate over a list of Periods and join the periods that overlap and maintain both periods if they don't overlap: interface Period { boolean…
alexpfx
  • 6,412
  • 12
  • 52
  • 88
7
votes
2 answers

How to merge List of Maps of Maps into a Map of Maps?

Could you help me with Java Streams? As you can see from the title I need to merge List>> into Map>. The list is represented as List>> and…
Pasha
  • 1,768
  • 6
  • 22
  • 43
7
votes
4 answers

Java 8 Streams reduce remove duplicates keeping the most recent entry

I have a Java bean, like class EmployeeContract { Long id; Date date; getter/setter } If a have a long list of these, in which we have duplicates by id but with different date, such as: 1, 2015/07/07 1, 2018/07/08 2, 2015/07/08 2,…
Nestor Milyaev
  • 5,845
  • 2
  • 35
  • 51
7
votes
2 answers

Comparator in collector in stream causes issues with type inference?

I have the following simplified example that groups a List of Strings into categories, in the form of a TreeMap from Integer to List public static void main(String[] args) { List list = Arrays.asList("A", "B", "C", "D", "E"); …
Rick
  • 935
  • 2
  • 7
  • 22
7
votes
1 answer

Is there a way to collect a map using "groupingBy" for MULTIPLE elements within a nested structure?

First, a bit of context code: import java.util.*; import java.util.concurrent.atomic.DoubleAdder; import java.util.function.Function; import java.util.stream.Collectors; class Scratch { static enum Id {A, B, C} static class IdWrapper { …
GhostCat
  • 137,827
  • 25
  • 176
  • 248
7
votes
2 answers

Java8 Collectors.groupingBy

I want to groupingBy a Collection based on the LocalDateTime, but I only want to get the Hour, not the minutes, seconds... .collect(Collectors.groupingBy(cp -> getUpdateLocalDate()) public LocalDate getUpdateLocalDate() { return…
Nuñito Calzada
  • 4,394
  • 47
  • 174
  • 301