collectors

What's the difference between groupingby and mapping in Collectors (Java)?

若如初见. 提交于 2021-02-06 08:43:34
问题 Take a look at this piece of code. // group by price, uses 'mapping' to convert List<Item> to Set<String> Map<BigDecimal, Set<String>> result = items.stream().collect( Collectors.groupingBy(Item::getPrice, Collectors.mapping(Item::getName, Collectors.toSet()) ) ); Is groupingBy and Mapping interchangeable? What is their differences? For the third parameter in collect(), would I get the same output type Map if I used Collectors.toList() instead of Collectors.toSet()? I heard that toList() is a

Sorting and Grouping on a list of objects

我的未来我决定 提交于 2021-01-28 05:08:12
问题 I have a List of Procedure objects as below Procedure1 01/01/2020 Procedure2 03/01/2020 Procedure3 03/01/2020 Procedure1 04/01/2020 Procedure5 05/01/2020, 02/01/2020 Procedure2 06/01/2020 and my Procedure class is like Class Procedure { List<Date> procedureDate; String procedureName; } I want to sort and group the objects based on the below conditions. All procedures should be grouped based on the procedure name. Procedures must be in descending order of procedure date. [first element in date

Sorting and Grouping on a list of objects

余生长醉 提交于 2021-01-28 05:06:55
问题 I have a List of Procedure objects as below Procedure1 01/01/2020 Procedure2 03/01/2020 Procedure3 03/01/2020 Procedure1 04/01/2020 Procedure5 05/01/2020, 02/01/2020 Procedure2 06/01/2020 and my Procedure class is like Class Procedure { List<Date> procedureDate; String procedureName; } I want to sort and group the objects based on the below conditions. All procedures should be grouped based on the procedure name. Procedures must be in descending order of procedure date. [first element in date

Collectors.groupby for Map<String,List<String>

孤街浪徒 提交于 2021-01-15 02:50:19
问题 Forgive me if the solution is very obvious but I can't seem to figure out how to do this public static void main(String[] args) { Map<String, String> map = new HashMap<>(); map.put("b1", "a1"); map.put("b2", "a2"); map.put("b3", "a1"); Map<String, List<String>> mm = map.values().stream().collect(Collectors.groupingBy(m -> m)); System.out.println(mm); } I want to group by based on values in hashmap. I want the output to be {a1=[b1, b3], a2=[b2]} but it is currently coming as {a1=[a1, a1], a2=

Collectors.groupby for Map<String,List<String>

倖福魔咒の 提交于 2021-01-15 02:42:25
问题 Forgive me if the solution is very obvious but I can't seem to figure out how to do this public static void main(String[] args) { Map<String, String> map = new HashMap<>(); map.put("b1", "a1"); map.put("b2", "a2"); map.put("b3", "a1"); Map<String, List<String>> mm = map.values().stream().collect(Collectors.groupingBy(m -> m)); System.out.println(mm); } I want to group by based on values in hashmap. I want the output to be {a1=[b1, b3], a2=[b2]} but it is currently coming as {a1=[a1, a1], a2=

Java 8 Stream Collectors - Collector to create a Map with objects in multiple buckets

我是研究僧i 提交于 2020-12-05 20:26:32
问题 The following code works and is readable but it seems to me I have intermediate operations that feel like they shouldn't be necessary. I've written this simplified version as the actual code is part of a much larger process. I've got a Collection of Widget , each with a name and multiple types (indicated by constants of the WidgetType enum). These multiple types are gettable as a Stream<WidgetType> though, if necessary, I could return those as some other type. (For various reasons, it is

Java 8 Stream Collectors - Collector to create a Map with objects in multiple buckets

人盡茶涼 提交于 2020-12-05 20:23:09
问题 The following code works and is readable but it seems to me I have intermediate operations that feel like they shouldn't be necessary. I've written this simplified version as the actual code is part of a much larger process. I've got a Collection of Widget , each with a name and multiple types (indicated by constants of the WidgetType enum). These multiple types are gettable as a Stream<WidgetType> though, if necessary, I could return those as some other type. (For various reasons, it is

Java 8 Stream Collectors - Collector to create a Map with objects in multiple buckets

一个人想着一个人 提交于 2020-12-05 20:21:37
问题 The following code works and is readable but it seems to me I have intermediate operations that feel like they shouldn't be necessary. I've written this simplified version as the actual code is part of a much larger process. I've got a Collection of Widget , each with a name and multiple types (indicated by constants of the WidgetType enum). These multiple types are gettable as a Stream<WidgetType> though, if necessary, I could return those as some other type. (For various reasons, it is