java-stream

Is there any way to convert a 2D List to 1D List using only `map` not using `flatMap`?

你。 提交于 2020-12-30 08:34:11
问题 I'm a Java beginner, I just got learn about map and flatMap . When 2d List should be converted to 1d List, it was implemented like below. List<List<Integer>> list_2d = List.of(List.of(1, 2), List.of(3, 4)); List<Integer> lst1 = list_2d .stream() .flatMap(arr -> arr.stream()) .collect(Collectors.toList()); printAll(lst1); // [1, 2, 3, 4] But, I think it looks that it can be implemented not using flatMap . Are there any way to make the code with same logic, just using map , not using flatMap ?

Java 8 Strem filter map in map — Map<String,Map<String,Employee>>

你。 提交于 2020-12-29 15:01:27
问题 How to filter a Map<String,Map<String,Employee>> using Java 8 Filter? I have to filter only when any of employee in the list having a field value Gender = "M". Input: Map<String,Map<String,Employee>> Output: Map<String,Map<String,Employee>> Filter criteria: Employee.genter = "M" Also i have to return empty map if the filtered result is empty. I tried the below, but it is not working as expected. It is returning the only if all the Employees are with gender "M". tempCollection.entrySet()

Flattening a list of elements in Java 8 Optional pipeline

做~自己de王妃 提交于 2020-12-29 13:19:54
问题 I have a id value which can be null . Then I need to call some service with this id to get a list of trades and fetch the first not null trade from the list. Currently I have this working code Optional.ofNullable(id) .map(id -> service.findTrades(id)) .flatMap(t -> t.stream().filter(Objects::nonNull).findFirst()) .orElse(... default value...); Is it possible to implement a line with a flatMap call more elegantly? I don't want to put much logic in one pipeline step. Initially I expected to

Flattening a list of elements in Java 8 Optional pipeline

你说的曾经没有我的故事 提交于 2020-12-29 13:19:12
问题 I have a id value which can be null . Then I need to call some service with this id to get a list of trades and fetch the first not null trade from the list. Currently I have this working code Optional.ofNullable(id) .map(id -> service.findTrades(id)) .flatMap(t -> t.stream().filter(Objects::nonNull).findFirst()) .orElse(... default value...); Is it possible to implement a line with a flatMap call more elegantly? I don't want to put much logic in one pipeline step. Initially I expected to

Flattening a list of elements in Java 8 Optional pipeline

泪湿孤枕 提交于 2020-12-29 13:18:37
问题 I have a id value which can be null . Then I need to call some service with this id to get a list of trades and fetch the first not null trade from the list. Currently I have this working code Optional.ofNullable(id) .map(id -> service.findTrades(id)) .flatMap(t -> t.stream().filter(Objects::nonNull).findFirst()) .orElse(... default value...); Is it possible to implement a line with a flatMap call more elegantly? I don't want to put much logic in one pipeline step. Initially I expected to

Rewrite double nested for loop as a Java 8 stream

匆匆过客 提交于 2020-12-29 12:20:29
问题 I have the following Java method: public List<GrantedAuthority> toAuthorities(Set<Role> roles) { List<GrantedAuthority> authorities = new ArrayList<>(); if (null != roles) { for (Role role : roles) { for (Permission permission : role.getPermissions()) { authorities.add(new SimpleGrantedAuthority("ROLE_" + permission.getLabel())); } } } return authorities; } I'm trying to rewrite it using Java 8 streams. My best attempt thus far: public List<GrantedAuthority> toAuthorities(Set<Role> roles) {

Java 8 stream sum entries for duplicate keys

故事扮演 提交于 2020-12-29 12:12:50
问题 I am using Java 8 streams to group a list of entries by a certain key and then sorting the groups by date. What I would like to do in addition is to "collapse" any two entries within a group that have the same date and sum them up. I have a class like this (stripped down for example purposes) class Thing { private String key; private Date activityDate; private float value; ... } Then I'm grouping them like so: Map<String, List<Thing>> thingsByKey = thingList.stream().collect( Collectors

Collectors.toList() showing error “Expected 3 argument but found 1”

你离开我真会死。 提交于 2020-12-29 08:12:16
问题 Why is my collect Collectors.toList() showing this error: Expected 3 argument but found 1 package com.knoldus; import java.util.Arrays; import java.util.List; import java.util.stream.Collector; import java.util.stream.Collectors; import java.util.stream.IntStream; interface Cityeration<T> { public abstract List<T> Cityeration(List<T> first, List<T> Second); } public class ListMultiplication { public static void main(String s[]) { List firstList = Arrays.asList(1, 2, 3, 4, 5); List secondList

Collectors.toList() showing error “Expected 3 argument but found 1”

孤者浪人 提交于 2020-12-29 08:01:51
问题 Why is my collect Collectors.toList() showing this error: Expected 3 argument but found 1 package com.knoldus; import java.util.Arrays; import java.util.List; import java.util.stream.Collector; import java.util.stream.Collectors; import java.util.stream.IntStream; interface Cityeration<T> { public abstract List<T> Cityeration(List<T> first, List<T> Second); } public class ListMultiplication { public static void main(String s[]) { List firstList = Arrays.asList(1, 2, 3, 4, 5); List secondList

Split list of objects into multiple lists of fields values using Java streams

☆樱花仙子☆ 提交于 2020-12-29 07:51:33
问题 Let's say I have such object: public class Customer { private Integer id; private String country; private Integer customerId; private String name; private String surname; private Date dateOfBirth; } and I have a List<Customer> . I would like to split such list with Java streams so that I would get a list of ids List<Integer> , countries List<String> , customerIds List<Integer> etc. I know that I could do it as simple as making 6 streams such as: List<Integer> idsList = customerList.stream()