java-stream Questions

6

Solved

How can I split odd and even numbers and sum both in a collection using stream methods of Java 8? public class SplitAndSumOddEven { public static void main(String[] args) { // Read the input t...
Cleancut asked 22/1, 2016 at 5:10

9

Solved

I have read a lot about Java 8 streams lately, and several articles about lazy loading with Java 8 streams specifically: here and over here. I can't seem to shake the feeling that lazy loading is C...
Angioma asked 7/10, 2018 at 4:46

15

My exact scenario is inserting data to database in batches, so I want to accumulate DOM objects then every 1000, flush them. I implemented it by putting code in the accumulator to detect fullness ...
Edam asked 20/12, 2014 at 19:33

7

Solved

In java 8, what's the best way to check if a List contains any duplicate? My idea was something like: list.size() != list.stream().distinct().count() Is it the best way?
Stonemason asked 5/5, 2015 at 12:50

6

Solved

I have the following expression: scheduleIntervalContainers.stream() .filter(sic -> ((ScheduleIntervalContainer) sic).getStartTime() != ((ScheduleIntervalContainer)sic).getEndTime()) .collect...
Hearsh asked 2/3, 2016 at 7:3

8

Solved

Is there a concise way to extract both the min and max value of a stream (based on some comparator) in one pass? There appear to be many ways to get the min and max values individually, or I can s...
Engrain asked 23/1, 2017 at 21:46

7

Solved

My colleague and I had a bug that was due to our assumption that an empty stream calling allMatch() would return false. if (myItems.allMatch(i -> i.isValid()) { //do something } Of course,...
Merocrine asked 13/5, 2015 at 18:53

5

Solved

I have the following code which could be much simpler using Java 8 stream API: List<List<String>> listOfListValues; public List<String> getAsFlattenedList() { List<String>...
Fawnfawna asked 28/11, 2018 at 9:3

6

Solved

I want to implement a Stream<T>. I don't want to just use implements Stream<T>, because I would have to implement a ton of methods. Can this be avoided? To be more concrete, how can ...
Biggers asked 6/6, 2015 at 17:27

5

Solved

Does someone know how I would write a sequence processing in a java stream API manner in Python ? The idea is to write the operations in the order they will happen: myList.stream() .filter(conditi...
Homologize asked 10/2, 2019 at 15:38

6

Solved

Why does this throw a java.lang.NullPointerException? List<String> strings = new ArrayList<>(); strings.add(null); strings.add("test"); String firstString = strings.stream() ...
Multiplicity asked 8/9, 2015 at 20:36

5

Solved

Such as in .Net, which provides several implementations of the Action delegate (equivalent to Java Consumer functional interface) with different number and type of arguments, I was expecting that J...
Jewelry asked 16/7, 2014 at 11:8

11

Solved

I have a list of Integer list and from the list.stream() I want the maximum value. What is the simplest way? Do I need comparator?
Gyrostat asked 13/7, 2015 at 8:10

14

Solved

Collectors.toMap throws a NullPointerException if one of the values is null. I don't understand this behaviour, maps can contain null pointers as value without any problems. Is there a good reason ...
Khelat asked 8/7, 2014 at 11:44

4

Solved

Suppose we have two streams as follows: IntStream stream1 = Arrays.stream(new int[] {13, 1, 3, 5, 7, 9}); IntStream stream2 = Arrays.stream(new int[] {1, 2, 6, 14, 8, 10, 12}); stream1.merge(stream...
Selfish asked 30/9, 2018 at 4:0

7

Solved

How do I sort string values in case-insensitive order in the following? List<Employee> listofEmployees = Arrays.asList( new Employee(1, "aaa", Arrays.asList(123, 345, 678)), new Employee(1...
Permeate asked 13/4, 2018 at 16:50

12

Solved

How to implement "partition" operation on Java 8 Stream? By partition I mean, divide a stream into sub-streams of a given size. Somehow it will be identical to Guava Iterators.partition()...
Careen asked 7/9, 2015 at 8:50

10

Solved

Suppose I have a method that returns a read-only view into a member list: class Team { private List<Player> players = new ArrayList<>(); // ... public List<Player> getPlayers...
Eberhardt asked 10/7, 2014 at 12:42

5

Solved

I have a Stream<Set<Integer>> intSetStream. I can do this on it... Set<Integer> theSetWithTheMax = intSetStream.max( (x,y)->{ return Integer.compare( x.size(), y.size() ); } ...
Mommy asked 11/4, 2018 at 19:6

4

Solved

I am getting Cannot infer type arguments for java.util.HashMap<> for the following code class Test { public static void main(String[] args) { Map<Integer, String> map = new Ha...
Algebraist asked 23/11, 2017 at 7:17

2

Solved

I have a list of orders and I want to group them by user using Java 8 stream and Collectors.groupingBy: orderList.stream().collect(Collectors.groupingBy(order -> order.getUser()) This return ...
Rundlet asked 14/6, 2016 at 14:57

35

Solved

In Java 8 how can I filter a collection using the Stream API by checking the distinctness of a property of each object? For example I have a list of Person object and I want to remove people with ...
Amaris asked 16/5, 2014 at 15:40

7

Solved

Is it possible to cast a stream in Java 8? Say I have a list of objects, I can do something like this to filter out all the additional objects: Stream.of(objects).filter(c -> c instanceof Clien...
Contrite asked 19/3, 2014 at 16:9

6

I have a List of Integer say list1, and I want to get another list list2 which will contain the cumulative sum up until the current index from start. How can I do this using Stream API java 8 ? Li...
Nardone asked 20/3, 2019 at 16:32

4

I have a problem which I feel would be perfect for streams and/or lambdas. On the other hand I don't want to overcomplicate this, but since will use this specific technique in many variations (run ...
Cutch asked 3/4, 2017 at 19:2

© 2022 - 2025 — McMap. All rights reserved.