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

Number of occurrence IntStream Java

I want to define a method named countRepeats that takes in a List of digits 0 to 9 and returns the number of occurrences of adjacent repeated letters. For example, Test case 1: the array {0, 1, 2, 2, 1, 2, 2, 1, 3, 3, 1} has three occurrences of…
user10320581
7
votes
4 answers

Count elements from Stream but consider only N for collecting

Is the following lambda possible somehow in Java? I'd like to count elements from my filtered stream but collaterally store the first 10 stream().filter(myFilter) //Reduces input to forthcoming operations .limit(10) //Limits to ten…
Whimusical
  • 6,401
  • 11
  • 62
  • 105
7
votes
4 answers

Java 8: How to convert String to Map?

I have a Map: Map utilMap = new HashMap(); utilMap.put("1","1"); utilMap.put("2","2"); utilMap.put("3","3"); utilMap.put("4","4"); I converted it to a String: String utilMapString = utilMap .entrySet() …
Phuong
  • 329
  • 2
  • 7
  • 15
7
votes
3 answers

Getting the result of a Lambda in java

I was wondering how to refer to the result of a lambda in Java? This is so I can store the results into an ArrayList, and then use that for whatever in the future. The lambda I have is: try { Files.newDirectoryStream(Paths.get("."),path ->…
7
votes
5 answers

How to compute Map from stream, to then check property of map values?

My requirement: I have an interface that shall only contain entries such as public final static short SOME_CONST = whatever. The catch: the short constants need to be unique. And in when there are duplicates, I am mainly interested in having the…
GhostCat
  • 137,827
  • 25
  • 176
  • 248
7
votes
3 answers

Spliterator vs Stream.Builder

I read some questions how to create a finite Stream ( Finite generated Stream in Java - how to create one?, How do streams stop?). The answers suggested to implement a Spliterator. The Spliterator would implement the logic how to and which element…
LuCio
  • 5,055
  • 2
  • 18
  • 34
7
votes
1 answer

Invoke method from stream api in java

I am novice to java 8.I am trying below scenario. class Numbers{ private Long userId; private Long number1; private Long number2; } List list = new ArrayList(); Input == { "userId":1, "number1":10, "number2":20 } { …
Ankita Goyal
  • 391
  • 1
  • 2
  • 16
7
votes
2 answers

Using streams to apply different functions on key value pair depending on keys

Currently the code use plain old foreach loop String preEvalObj = new String("123"); for(Map.Entry entry : someHashMap.entrySet()){ String key = entry.getKey(); Float value = entry.getValue(); if(preEvalObj.equals(key)){ …
DsCpp
  • 2,259
  • 3
  • 18
  • 46
7
votes
2 answers

How can I use Java 8 streams to sort an ArrayList of objects by a primitive int member?

Here is an example class. I know the simplest thing would be to change the members from primitive type int to object Integer and use stream/lambda/sorted, but there may be reasons to only have a primitive type int such as space. How could I use the…
7
votes
4 answers

Different behavior between lambda expression and method reference by instantiation

As I know lambda expression can be replaced by method reference without any issues. My IDEs say the same, but the following example shows the opposite. The method reference clearly returns the same object, where as lambda expression returns new…
st.ebberr
  • 153
  • 2
  • 6
7
votes
4 answers

How return null by using Stream API?

So, I have an ArrayList of autogenerated strings. I want to find the first element that contains some character or else if there is no one element that matches this filter, to apply another filter. In another way, I want to return null object. So I…
Roman Shmandrovskyi
  • 883
  • 2
  • 8
  • 22
7
votes
2 answers

Why does chaining .map() and .filter() in Kotlin not work as expected?

I am trying to map a list of objects to a list of objects of another type, then filter the list, then map to a list of a third type, just like I would do by chaining streams in Java 8+ (I changed the class and variable names to make it more…
Pierre Henry
  • 16,658
  • 22
  • 85
  • 105
7
votes
1 answer

Mutation reductions for parallelStream in Java 8

Joshua Bloch in (Third Edition) mentions that The operations performed by Stream’s collect method, which are known as mutable reductions, are not good candidates for parallelism because the overhead of combining collections is…
Hearen
  • 7,420
  • 4
  • 53
  • 63
7
votes
1 answer

Combining multiple java streams in a structured way

I want to use Java's stream API to do some calculations on a list of objects: List.stream()... The Item class contains many attributes. For some of those I need to take the average value across all items in the collection, for other attributes…
user1884155
  • 3,616
  • 4
  • 55
  • 108
7
votes
2 answers

Why are Java 8 Streams only available from API level 24?

Supported Java 8 Language Features and APIs states that we can use default methods and lambda expressions in android projects with any min sdk API level. However, the Stream library (java.util.stream) is only supported for API 24 and higher. Can…
AAryz
  • 140
  • 7