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

How to merge two arrays into a map using Java streams?

Lets suppose we were given the following two arrays String[] keys = new String[] {"a", "b", "c", "aa", "d", "b"} int[] values = new int[] { 1 , 2 , 3 , 4 , 5 , 6 } And by merging these 2 arrays into HashTable we get the following //…
Jamie
  • 99
  • 2
  • 9
7
votes
2 answers

How to use Grouping and sorting together in JAVA 8

I am trying to sort a list and then group the elements. Problem is that when I group the elements sorting order is going for a toss. Below is my piece of code listOfRooms.stream().sorted(Comparator.comparing(DXARoom::getStayAverageRate)) …
Abhishek Galoda
  • 2,753
  • 24
  • 38
7
votes
2 answers

Java 8 streams - How do I manipulate value in a group by result?

I have a list of objects with two String fields like Name1, Active Name2, InActive Name3, InActive Name4, Active And I would like to have a Map> where Active=true and InActive=false. I tried active =…
Bick
  • 17,833
  • 52
  • 146
  • 251
7
votes
2 answers

Using Java 8 Stream Reduce to return List after performing operation on each element using previous elements values

I'm new to Streams and Reduce so I'm trying it out and have hit a problem: I have a list of counters which have a start counter and end counter. The startcounter of an item is always the endcounter of the previous. I have a list of these counters…
user1501171
  • 210
  • 1
  • 3
  • 17
7
votes
1 answer

Stream anyMatch with List

I have a list of "items" and each item has a property of item.posts (which is a list of post-instances). I want to filter my "item"-list by two properties: If "item.isBig" and if any post of a item is enabled, then collect the returned Stream.…
nimo23
  • 5,170
  • 10
  • 46
  • 75
7
votes
2 answers

The min non-null LocalDateTime of a list using stream

I know how to get the min LocalDateTime of a list thanks to: https://stackoverflow.com/a/20996009/270143 e.g. LocalDateTime minLdt = list.stream().map(u -> u.getMyLocalDateTime()).min(LocalDateTime::compareTo).get(); My problem is slightly different…
ycomp
  • 8,316
  • 19
  • 57
  • 95
7
votes
1 answer

Why is the reduce combiner function not executed?

I am new to Java 8. I am learning stream API's reduce method. I am seeing a weird behavior with this code: public class PrdefinedCollectors { public static void main(String[] args) { Stream stream = Stream.of(1, 2, 3, 4, 5,…
vicky
  • 2,119
  • 4
  • 18
  • 32
7
votes
3 answers

Transform List into Map using only two keys and odd or even list indexes as values - Java 8 Stream

I would like to transform list into map using as key values only two string values. Then as values just list of strings containing elements from odd or even index positions from input list. Here is old fashion code: Map> map =…
wokadakow
  • 171
  • 11
7
votes
4 answers

what does java8 stream map do here?

I was confused about the difference between map() and forEach() method in java8 stream. For instance, List strings = Lists.newArrayList("1", "2"); Map map = Maps.newHashMap(); strings.stream().map(s->map.put(s,…
Sin Chang
  • 81
  • 1
  • 5
7
votes
3 answers

Stream.reduce always preserving order on parallel, unordered stream

I've gone through several previous questions like Encounter order preservation in java stream, this answer by Brian Goetz, as well as the javadoc for Stream.reduce(), and the java.util.stream package javadoc, and yet I still can't grasp the…
George Aristy
  • 1,373
  • 15
  • 17
7
votes
1 answer

Java 8 stream, obtain average age by country

I'm trying to get into functional programming on java 8 User class public class User { private String name; private Country country; private int age; // setters - getters } Country class public class Country { private String…
user5830145
7
votes
3 answers

Getting the elements from a list of lists with the Java Stream API in Kotlin

The following code (written in Kotlin) extracts the elements from a list of lists. It works, but looks rather ugly and difficult to read. Is there a nicer way to write the same with the java stream api? (Examples can be given in Kotlin or Java) val…
Fabian
  • 1,982
  • 4
  • 25
  • 35
7
votes
4 answers

java 8 findFirst vs map on optional

Given this code: class Foo { Integer attr; public Integer getAttr() {return attr;} } List list = new ArrayList<>(); list.add(new Foo()); list.stream().map(Foo::getAttr).findAny().orElse(null); …
7
votes
1 answer

java 8 parallel stream, blockingcode possible?

My situation is quite simple. I have a list I want to perform logic on each item asynchronically. when all the threads are done, i want to call a close connection. like so: bucketsList.parallelStream().forEach(t -> { //some logic }); try { …
sharon gur
  • 343
  • 5
  • 22
7
votes
3 answers

Inverted Collectors.toMap to add to an ArrayList

I would like to put the frequencies of the numbers in a TreeMap with the frequencies as the keys and the numbers that have that frequency in an ArrayList. I have two problems: 1) I'm getting a "non-static methods cannot be referenced from a static…
John Estess
  • 576
  • 10
  • 22
1 2 3
99
100