Questions tagged [groupingby]

77 questions
52
votes
1 answer

sorting Lists after groupingBy

I am wondering, if there is already an implemented feature in streams (or Collectors) which has sorted Lists as values. E.g. the following codes both produce gender-grouped Lists of persons, sorted by age. The first solution has some overhead…
ctst
  • 1,610
  • 1
  • 13
  • 28
4
votes
3 answers

Java Stream GroupBy and Reduce

I have an Item class which contains a code, quantity and amount fields, and a list of items which may contain many items (with same code). I want to group the items by code and sum up their quantities and amounts. I was able to achieve half of it…
hakuna matata
  • 3,243
  • 13
  • 56
  • 93
4
votes
1 answer

How to use groupingBy on nested lists

I am facing a tricky situation where I have to use groupingBy on an object which has nested lists. I tried few things with map(), flatmap(), toMap() but not able to come up with a solution and just going in circles. Any help here from the stream…
Prasad
  • 368
  • 1
  • 6
  • 16
4
votes
4 answers

Java 8 stream grouping a List> by the same to a new List>

I have a List> such as: Map m1 = new HashMap<>(); m1.put("date", "2020.1.5"); m1.put("B", "10"); Map m2 = new HashMap<>(); m2.put("date", "2020.1.5"); m2.put("A", "20"); Map m3 =…
Better Man
  • 51
  • 1
  • 5
3
votes
3 answers

Java-Stream - Create a List of aggregated Objects using Collector groupingBy

I have a domain class View: public class View { private String id; private String docId; private String name; // constructor, getters, etc. } And there's a list of View objects. Elements having the same id, only differ in one…
3
votes
4 answers

How to group Objects by property using using collector groupingBy() on top of flatMap() in Java 8

How to create Map> of below. Here, String (key of the Map) is the category of a Product. One product can belong to multiple categories, like in the example below. I am trying with below code, however not able to get next…
Shiva
  • 1,962
  • 2
  • 13
  • 31
3
votes
2 answers

Java-Stream - Collect a List of objects into a Set of Strings after applying Collector groupingBy

Given the following classes Order and Item and a list of Orders. @Getter @AllArgsConstructor @ToString public class Order { String customerName; List items; } @Getter @AllArgsConstructor @ToString @EqualsAndHashCode public class Item…
wannaBeDev
  • 516
  • 3
  • 14
3
votes
1 answer

How can I get some statistics using java 8 stream with object list

I have a List. I need to get a List from the aggregation of SqlResult. For example, SqlResult(Input) public class SqlResult{ private String key1; private String key2; private Double val; // getters, setters and…
3
votes
2 answers

Transforming/GroupingBy values inside List

Given List where each Student has List, group by Book for List Working Solution (Uses Java 9) I already have a working code mentioned below public class Demo { public static void main(String[] args) { List
Ajeetkumar
  • 1,271
  • 7
  • 16
  • 34
3
votes
1 answer

Java 8: grouping by multiple fields and then sort based on value

I have a class called Info public class Info { private String account; private String opportunity; private Integer value; private String desc; } I want to group a list of Info, by two fields(account and then opportunity) and then…
wasim sarkar
  • 33
  • 1
  • 5
2
votes
2 answers

Collectors.groupingBy - collecting to list of some property of Object instead of list of Object itself

Lets say I have a class public class FootBallTeam { private String name; // team name private String league; //getters, setters, etc. } I'm trying to group Teams names according to their League. For example: League A -> [nameOfTeam2,…
Save Soil
  • 75
  • 7
2
votes
2 answers

Group strings into multiple groups when using stream groupingBy

A simplified example of what I am trying to do: Suppose I have a list of strings, which need to be grouped into 4 groups according to a condition if a specific substring is contained or not. If a string contains Foo it should fall in the group FOO,…
bbKing
  • 179
  • 1
  • 8
2
votes
2 answers

How to Group a List of objects by Two fields and obtain a List of objects of resulting type

I have a list of objects shown below: public class AssignDTO { private String assignmentIbanId; private String agreementFileId; private BankData bankData; // getter and setter } [ { …
Aymen Kanzari
  • 1,765
  • 7
  • 41
  • 73
2
votes
2 answers

How to preserve all Subgroups while applying nested groupingBy collector

I am trying to group a list of employees by the gender and department. How do I ensure all departments are included in a sorted order for each gender, even when the relevant gender count is zero? Currently, I have the following code and…
Roshana
  • 357
  • 1
  • 3
  • 17
1
2 3 4 5 6