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 8 stream sum entries for duplicate keys

I am using Java 8 streams to group a list of entries by a certain key and then sorting the groups by date. What I would like to do in addition is to "collapse" any two entries within a group that have the same date and sum them up. I have a class…
cloudwalker
  • 2,346
  • 1
  • 31
  • 69
7
votes
1 answer

Is there Java stream equivalent to while with variable assignment

Is there any stream equivalent to the following List ints; while (!(ints = this.nextInts()).isEmpty()) { // do work }
Jan Tajovsky
  • 1,162
  • 2
  • 13
  • 34
7
votes
2 answers

Filter nested object java 8

I have these classes class LivingOrganism { List listOfDomain; } class Domain { List listOfSpecies; } class Species { List listOfColor; } class Color { String color; } From the top to bottom, it won't have…
stackyi
  • 95
  • 1
  • 2
  • 7
7
votes
6 answers

Find which element of the stream does not match the given predicate in allmatch

I want to find out which element is failing the predicate in case of allmatch. List numbers = Arrays.asList(1, 2, 3, 4, 5); boolean isEven = numbers.stream().allMatch(n-> n % 2 == 0); Here isEven is false, as element 1 fails the…
sanchitkum
  • 333
  • 2
  • 6
  • 18
7
votes
3 answers

Stream over a List of Map and collect specific key

This is my List: [ {name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60} ]; I want to pluck name values and create another List like this: ["moe", "larry", "curly"] I've written this code and it…
Snow
  • 71
  • 1
  • 3
7
votes
3 answers

How do I sort a List of TreeSets with java8 streams

My list contains sets like [1,3,5][2,6,4] etc, all of the same size. I tried doing this but it doesn't seem to work. List> block; for(TreeSet t : block){ …
LexByte
  • 382
  • 4
  • 15
7
votes
1 answer

non-interference requirement on Java 8 streams

I am a beginner in Java 8. Non-interference is important to have consistent Java stream behaviour. Imagine we are process a large stream of data and during the process the source is changed. The result will be unpredictable. This is …
7
votes
3 answers

How to convert 2D list to 1D list with Streams?

I've tried this code (list is ArrayList>): list.stream().flatMap(Stream::of).collect(Collectors.toList()); but it doesn't do anything; the list is still a 2D list. How can I convert this 2D list to a 1D list?
ack
  • 1,181
  • 1
  • 17
  • 36
7
votes
2 answers

How to get n first values from an Iterator in Java 8?

I have sorted a HashMap using Sort a Map by values (Java) to that I have a LinkedHashMap, i.e. an Iterable which garantees iteration order. Now, I'd like to retrieve a java.util.List of the first n entries of the map with a one-liner, if…
Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
7
votes
2 answers

Java 8 streams group by 3 fields and aggregate by sum and count produce single line output

I know there a similar questions asked in the forum but none of them seem to be addressing my problem fully. Now I'm very new to Java 8, so please bear with me. I have a list of Products, for example: Input: name category type cost prod1 …
Vuzi
  • 185
  • 2
  • 4
  • 13
7
votes
1 answer

migrate from jack to native Java 8

I'm trying to use lambda expressions and streams in my Android project.I'm using streamsupport library for streams, and native Java 8 for lambda expressions. To be able to use Java 8 features I need to add compileOptions { sourceCompatibility…
GiorgiSh
  • 127
  • 11
7
votes
1 answer

Why filter with side effects performs better than a Spliterator based implementation?

Regarding the question How to skip even lines of a Stream obtained from the Files.lines I followed the accepted answer approach implementing my own filterEven() method based on Spliterator interface, e.g.: public static Stream
Miguel Gamboa
  • 8,855
  • 7
  • 47
  • 94
7
votes
1 answer

elasticsearch: convert StreamOutput to String

I'm overriding FilterClient so I can see incoming requests. I'd like some way to get a String representation of the ActionRequest that's passed in. ActionRequest let's you write to a StreamOuput, which is an Elasticsearch type that is a subclass of…
lf215
  • 1,185
  • 7
  • 41
  • 83
7
votes
1 answer

Java 8 lambda Collectors.groupingBy with map in Collector.toList()

I have implemented the following example: Map> map = events.getItems().stream() .collect(Collectors.groupingBy(Event::getStatus, Collectors.toList())); How can I get an output of Map> map…
Sergii
  • 7,044
  • 14
  • 58
  • 116
7
votes
3 answers

What is the best way of filtering by multiple fields in java

I have an entity with 10 fields: Class Details{ String item; String name; String type; String origin; String color; String quality; String country; String quantity; Boolean availability; String price; } I have a restful…
user171943
  • 1,325
  • 3
  • 12
  • 18
1 2 3
99
100