Category "java-stream"

How to use Map.computeIfAbsent() in a stream?

I have a fun puzzler. Say I have a list of String values: ["A", "B", "C"] Then I have to query another system for a Map<User, Long> of users with an attr

Find Indices of the pair of elements in the Array such that they add up to the Target value

I want to find a target sum in an array by adding integers until it's reached, then return the indexes which add up to the target by using streams. For example,

Rewrite imperative code of creating and populating Map in a declarative way using Java Stream API

I have this piece of code : Map<String, BillingAccount> billingAccountsMap = new HashMap<>(); for (BillingAccount ba : billingAccounts) { if (ba

Why stream created with Spliterators is not being processed in parallel?

This is probably very basic, but I am not a Java person. Here is my processing code which simply prints and sleeps: private static void myProcessings(int va

How can I make stream with fixed arguments along with varargs? [duplicate]

Let's say we have the following method. void some(int id, int... otherIds) { } How can I create a single IntStream with those two arguments?

Java streams collect excel CSV to a list filtering based on the sum of a column

Suppose we have an excel spreadsheet that looks like: StatusCount FirstName LastName ID 1 Tod Mahones 122145 0 Tod Maho

How to map elements to their index using streams?

I get a stream of some custom objects and I would like to create a map Map<Integer, MyObject> with index of each object as key. To give you a simple examp

Count and print unique list items in one chain using Java Streams

I'm trying to achieve this using only functional programming constructs (Streams, Collectors, lambda expressions). Let's say list is a String[]: {"Apple", "Sa

filter objects based on an ID and Date Java

I have a class that for which each instance has a unique Id, a name and update date. ID NAME UPDATE DATE 1200 ANNA* 2022-03-01 1300 JONH* 2022-04-01 1200 ANNA*

Java 8 Streams - How to get an equivalent of partitioningBy() with Integer keys?

In the code shown below, p is a predicate of type Predicate<String>. Map<Boolean, List<String>> partitioned = numbers.stream() .collect(Co

How large should my list of objects be to warrant the use of java 8's parallelStream?

I have a list of objects from the database and i want to filter this list using the filter() method of the Stream class. New objects will be added to the databa

Unable to assign the result returned by the Method that receives a generic List and returns a List<?>

I'm trying to filter a List of objects that implements an interface. And I'm trying to create a generic method for all the classes. Something like: interface So

Parallelism with Streams created from Iterators

Experimenting with streams I ran into the following behavior which I don't quite understand. I created a parallel stream from an iterator and I noticed that it

How can I reverse a single String in Java 8 using Lambda and Streams?

I have one string say "Aniruddh" and I want to reverse it using lambdas and streams in Java 8. How can I do it?

How to log a message if a stream is empty within the Stream?

Given the following Java 8 Stream: scheduleService.list().stream() .filter(Schedule::getEnabled) .filter(this::runn

Avoid ConcurrentModificationException on java stream using cache

I'm getting occasionally ConcurrentModificationException with the following code: public Set<MyObject> getTypes(Set<Type> names) { Set<My

How to limit number of parallel executions in ParallelStream?

list.parallelStream().forEach(element -> ...); How can I limit the number of parallel threads nowadays? There was a "hack" in the past to set a System prope

Collect both matching and non-matching in one stream processing?

Is there a way to collect both matching and not matching elements of stream in one processing? Take this example: final List<Integer> numbers = Arrays.as

How to check if list has more then 3 elements using Java8

I have a method that accepts a list of object. I want to iterate through that list and check if the list has more than 3 elements. If it does, I want to throw e

Java stream. Sum two fields in a stream of objects

I have something like this: Integer totalIncome = carDealer.getBrands().stream().mapToInt(brand -> brand.getManufacturer().getIncome()).sum(); Integer total