java-stream

How to convert nested for loop into Hashmap using java stream

ぐ巨炮叔叔 提交于 2020-02-24 08:42:22
问题 I am trying to convert the below nested for loop into hashmap using java stream but i got struck in the collector step. Could you please help? Existing code: private static HashMap<String, Long> getOutput(List<Employee> eList) { HashMap<String, Long> outputList = new HashMap<>(); for (Employee employee : eList) { List<Department> departmentList = employee.getDepartmentList(); for (Department department : departmentList) { if (department.getType().equals(DepartmentType.SCIENCE)) { outputList

How to send parameters to a reference method in a stream (java 8)?

我与影子孤独终老i 提交于 2020-02-22 08:12:21
问题 I have a list of activities(Activity) and I want to determine a data structure of the form Map(String, DateTime) (not Duration or Period; DateTime it's a must) that maps. For each activity the total duration computed over the monitoring period. The class Activity has: activityLabel(String) , startTime(DateTime) , endTime(DateTime) . I use joda time. This is what I have done: Map<String, DateTime> durations = activities.stream().collect(Collectors.toMap( it -> it.activityLabel, it ->new

How to send parameters to a reference method in a stream (java 8)?

百般思念 提交于 2020-02-22 08:10:13
问题 I have a list of activities(Activity) and I want to determine a data structure of the form Map(String, DateTime) (not Duration or Period; DateTime it's a must) that maps. For each activity the total duration computed over the monitoring period. The class Activity has: activityLabel(String) , startTime(DateTime) , endTime(DateTime) . I use joda time. This is what I have done: Map<String, DateTime> durations = activities.stream().collect(Collectors.toMap( it -> it.activityLabel, it ->new

How to send parameters to a reference method in a stream (java 8)?

久未见 提交于 2020-02-22 08:10:11
问题 I have a list of activities(Activity) and I want to determine a data structure of the form Map(String, DateTime) (not Duration or Period; DateTime it's a must) that maps. For each activity the total duration computed over the monitoring period. The class Activity has: activityLabel(String) , startTime(DateTime) , endTime(DateTime) . I use joda time. This is what I have done: Map<String, DateTime> durations = activities.stream().collect(Collectors.toMap( it -> it.activityLabel, it ->new

How to send parameters to a reference method in a stream (java 8)?

孤人 提交于 2020-02-22 08:09:31
问题 I have a list of activities(Activity) and I want to determine a data structure of the form Map(String, DateTime) (not Duration or Period; DateTime it's a must) that maps. For each activity the total duration computed over the monitoring period. The class Activity has: activityLabel(String) , startTime(DateTime) , endTime(DateTime) . I use joda time. This is what I have done: Map<String, DateTime> durations = activities.stream().collect(Collectors.toMap( it -> it.activityLabel, it ->new

How to send parameters to a reference method in a stream (java 8)?

浪子不回头ぞ 提交于 2020-02-22 08:09:28
问题 I have a list of activities(Activity) and I want to determine a data structure of the form Map(String, DateTime) (not Duration or Period; DateTime it's a must) that maps. For each activity the total duration computed over the monitoring period. The class Activity has: activityLabel(String) , startTime(DateTime) , endTime(DateTime) . I use joda time. This is what I have done: Map<String, DateTime> durations = activities.stream().collect(Collectors.toMap( it -> it.activityLabel, it ->new

How to send parameters to a reference method in a stream (java 8)?

二次信任 提交于 2020-02-22 08:09:22
问题 I have a list of activities(Activity) and I want to determine a data structure of the form Map(String, DateTime) (not Duration or Period; DateTime it's a must) that maps. For each activity the total duration computed over the monitoring period. The class Activity has: activityLabel(String) , startTime(DateTime) , endTime(DateTime) . I use joda time. This is what I have done: Map<String, DateTime> durations = activities.stream().collect(Collectors.toMap( it -> it.activityLabel, it ->new

Java 8 lambda create list of Strings from list of objects

依然范特西╮ 提交于 2020-02-13 04:55:32
问题 I have the following qustion: How can I convert the following code snipped to Java 8 lambda style? List<String> tmpAdresses = new ArrayList<String>(); for (User user : users) { tmpAdresses.add(user.getAdress()); } Have no idea and started with the following: List<String> tmpAdresses = users.stream().map((User user) -> user.getAdress()); 回答1: You need to collect your stream into a List: List<String> adresses = users.stream() .map(User::getAdress) .collect(Collectors.toList()); For more

Java 8 lambda create list of Strings from list of objects

你离开我真会死。 提交于 2020-02-13 04:55:30
问题 I have the following qustion: How can I convert the following code snipped to Java 8 lambda style? List<String> tmpAdresses = new ArrayList<String>(); for (User user : users) { tmpAdresses.add(user.getAdress()); } Have no idea and started with the following: List<String> tmpAdresses = users.stream().map((User user) -> user.getAdress()); 回答1: You need to collect your stream into a List: List<String> adresses = users.stream() .map(User::getAdress) .collect(Collectors.toList()); For more

Split a collection of objects based on a condition

老子叫甜甜 提交于 2020-02-06 07:52:25
问题 I have a list of objects to be added in a bag and bags capacity is 100 qty The Object and Bag look like below public class MyObject{ String id; int qty; } public class MyBag{ String id; int qty; } Is there any way to split MyObject in multiple MyBags grouping on the qty limit using Java 8 streams For example: myObjects are [myObject1:{id1, 150}, myObject2:{id2, 30}, myObject3:{id3, 150}] Since bag has a capacity of 100. Bags should be grouped as [ bag1:[{id1, 100}], bag2:[{id1, 50},{id2, 30},