Latest Questions tagged Java Stream

Browse all latest questions tagged Java Stream

Published 29 Oct, 2022
Java - Java stream groupingBy key as max salary employee and value as all employee of department

I have a list of Employee public class Employee { private String name; private Integer age; private Double salary; private Department department; } List<Employee> employeeList = Arrays...

Published 27 Oct, 2022
Java - How to replace only the first element of a List<Character> using java streams without making any changes to the rest of the list?

I am getting a List and I want to replace the first value in this list with '$' sign, So that I could use it in some place else. But i don't want the rest of the characters in the list to be misplaced...

Published 06 Sep, 2022
Java - How to call Java's Stream.collect() from groovy?

Having some background with FP from Scala I really don't like Groovy's names for collections methods. Given that and some architectural choices made above, I found using a Java 8 streams API (plus jav...

Published 15 Oct, 2022
Java - Filter a list with streams based on ids from another list

I have a list of Product objects with 30 objects. I have created a fix list with strings of ids. List&lt;String&gt; productsIdsForFreeSchoolYear = Arrays.asList("169", "172", "198", "213", "358", "4...

Published 20 Oct, 2022
Java - Java 8 stream and set attribute from call Boolean method

Please can you help me resolve this issue I am having when attempting to stream through an array list and call a setter based on a method which returns a Boolean. Written as a for loop, it would look...

Published 05 Sep, 2022
Java - Java Stream: divide into two lists by boolean predicate

I have a list of employees. They have isActive boolean field. I would like to divide employees into two lists: activeEmployees and formerEmployees. Is it possible to do using Stream API? What is the m...

Published 03 Oct, 2022
Java - Java 8 stream filter local variable

Obj1 x = null; String key = null; while(it.hasNext()){ x =; key = x.getKey(); -&gt; e.getKey().equals(key)).findFirst().get(); } This is complaining w...

Published 06 Sep, 2022
Java - Using streams to convert a list of objects into a string obtained from the toString method

There are a lot of useful new things in Java 8. E.g., I can iterate with a stream over a list of objects and then sum the values from a specific field of the Object's instances. E.g. public class ACl...

Published 04 Sep, 2022
Java - What is the difference between intermediate and terminal operations?

can someone tell me What is the difference between intermediate and terminal operations for Stream? Stream operations are combined into pipelines to process streams. All operations are either interme...

Published 01 Sep, 2022
Java - Java 8 Stream and operation on arrays

I have just discovered the new Java 8 stream capabilities. Coming from Python, I was wondering if there was now a neat way to do operations on arrays like summing, multiplying two arrays in a "one lin...

Published 14 Sep, 2022
Java - Adding two Java 8 streams, or an extra element to a stream

I can add streams or extra elements, like this: Stream stream = Stream.concat(stream1, Stream.concat(stream2, Stream.of(element)); And I can add new stuff as I go, like this: Stream stream = Strea...

Published 07 Sep, 2022
Java - Finite generated Stream in Java - how to create one?

In Java, one can easily generate an infinite stream with Stream.generate(supplier). However, I would need to generate a stream that will eventually finish. Imagine, for example, I want a stream of al...

Published 06 Sep, 2022
Java - Java 8 Stream - .max() with duplicates

So I have a collection of objects that have a step variable that can be 1 - 4. public class MyClass { private Long step; //other variables, getters, setters, etc. } Collection&lt;MyClass&g...

Published 06 Sep, 2022
Java - Files.walk(), calculate total size

I'm trying to calculate the size of the files on my disc. In java-7 this could be done using Files.walkFileTree as shown in my answer here. However if i wanted to do this using java-8 streams it will...

Published 04 Sep, 2022
Java - Difference between findAny() and findFirst() in Java 8

I am little confused between Stream#findAny() and Stream#findFirst() of the Stream API in Java 8. What I understood is that both will return the first matched element from the stream, for example, w...