Skip to content
Advertisement

Tag: java-stream

Stream API how to modify key and value in a map?

I have a String – Array map that looks like this What I need to do is Create a new map, where key – only numbers extracted from String like this ( key -> key.replaceAll(“\D+”,””); Value – first value from array like this value -> value[0]; Filter an array so that only this paris left where value > 0 I’ve

Custom Collector that cannot work in parallel

I have made a custom collector that uses a MessageDigest to create a hash. In general MessageDigest does not work in parallel. The issue I’m seeing is in the combiner() method. It is not possible to combine two MessageDigest objects. When I return null it seems to work but if I throw an UnsupportedOperationException it fails. What is the typical

Fail to write the reduce method using Stream API

I need to calculate the total price of all ordered dishes, but I fail to write the reduce method. This is a method signature ( argument has a map of Dish and how many times it was ordered ). So it must be something like this sum of every dish.getPrice * dishQuantaty The fail-code I was asked about Answer Did

Java 8 List from String and List of String

I want to generate a list of strings, comprising id and external ids, from a list of Bean. I got it using the below code, but here I need to do the stream twice. Is there any better way to rewrite this code? Answer Use Stream.concat inside the flatMap operation:

Java Streams; avoid finisher on Collectors.collectingAndThen

I’ve this code: Problem is that when ids is empty, this.practitionerRepository::findAllById is also executed. I’d like to avoid this step if resulting collector is empty. Any ideas? Answer Whilst the practical part of this question (how to avoid interrogating the repository with an empty list as an argument) is already addressed in other answers I want to point out that

Using Streams on a map and finding/replacing value

I’m new to streams and I am trying to filter through this map for the first true value in a key/value pair, then I want to return the string Key, and replace the Value of true with false. I have a map of strings/booleans: That is where I am stuck–I might be doing the first part wrong too, but I’m

How to divide a stream by object fields into two sets?

I have a stream, where each object is identified by a unique id. Moreover, each object has a either positive or negative Free values. I want to divide this stream into two Sets, where one contains the ids whose Free values was positive, and one with the rest. But I find the following not being the right way, as I’m

Trying to reverse my stream in ascending order – Java 8

This idea behind this code is that it reads the data from three .csv files w/ keys organized by year, and retrieves the sum of the data by year, as well as the minimum and maximum number for each year. The problem is, when print it returns the yearly results in descending order. I need help figuring out a way

Advertisement