Skip to content
Advertisement

Tag: java-stream

Map and groupBy values in a stream

I have a wrapper that may or may not contain data: I group one user to multiple contacts: It groups val mailsByUser: Map<String, List<EmailAdapter>> I want to group all emails to a unique user I want to unwrap the EmailAdapter so that the relation is EmailAdapter.user -> List<EmailAdapter.mail> or val mailsByUser: Map<String, List<Email>> I fail in the last step –

Java 11: convert List to TreeMap<String, List> using Collectors

I have a list like that I want to convert that List into a TreeMap<String, List<String>> like that: My code so far: I have two problems. First is that TreeMap::new is probably not working because the order is not the same as the original’s List. Second is that I don’t seem to find a way to make that List<String[]> into

Group by multiple fields and filter by common value of a field

I want to filter this Employee object based on the EmpPFcode . If collegeName has common value for 3 EmpPFcode, we will collect otherwise we will skip that records. So my result would be like below. Below one will skip because collageName is different. I try to do some logic below but it doesn’t not filter properly. Answer I. Solution:

parallel() not work with the 3-arg iterate()

Here is my code: I expect this will go parallel, but! When i run it, parallel() seems “dead”, numbers from 4 to 19 comes with a “perfect” sequence which was not i want. So i modified the “iterate” part like this: There comes fixed, the parallel() works again. So, Why??? The iterate-limit-parallel combo seems too silly. …Or in fact, there

Convert one Optional<List> to another Optional<List> in Java

How can I convert Optional List object from one type to another, for an example ProductMultipleOptionViewModel Type 1 Type 2 I want to convert from Optional<List<ProductMultipleOption>>to other Optional<List<ProductMultipleOptionViewModel>>. I tried the below code With the above code, I am not able to access the option value inside map method If product.getProductMultipleOption() is null return null or empty list. Answer You

Convert nested map of streams.groupingBy() into list of POJOs

I want to convert a nested map structure created by java streams + groupingBy into a list of POJOs, where each POJO represents one of the groups and also holds all the matching objects of that group. I have the following code: I use project lombok for convenience here (@Builder, @Data). Please let me know if that is confusing. My

Advertisement