Questions tagged [groupingby]

77 questions
2
votes
2 answers

How to aggregate grouped entities after stream groupingBy

I have a simple class Person: class Person { String firstName; String lastName; //getter, setter, constructor, toString } And an input list of Persons like: List myList = List.of( new Person("Helena", "Graves"), …
wannaBeDev
  • 516
  • 3
  • 14
2
votes
2 answers

can return java stream groupingby to this's array variable?

Q.1) hi, java stream's groupingby can make oneself's array varible? this is entity public class Test { private int id; private int itemId; private int[] itemIds; private boolean filter; } this is test List sample { test(id=1,…
gd k GD
  • 23
  • 4
2
votes
3 answers

Java Collectors Grouping By

I have an input of integers and I would like to sort all even numbers before all odd ones in ascending order and preserve the odds order. I am pretty sure I can achieve that with collectors and/or downstream collectors, but I am not sure how to do…
2
votes
2 answers

Java Stream Grouping by multiple fields individually in declarative way in single loop

I googled for it but I mostly found cases for grouping by aggregated fields or on to alter response of stream but not the scenario below: I have a class User with fields category and marketingChannel. I have to write a method in the declarative…
2
votes
2 answers

Stream collectingAndThen get latest record to sum a value

Currently, I'm facing with below dataset. My aim is to get the latest sum of Column4 group by the first two-column. // Column5 = version new Foo(1, "bbb", "cccc", 111, 0) new Foo(1, "bbb", "cccc", 234, 1) // latest new Foo(1, "bbb", "dddd", 111,…
Vincent Tang
  • 137
  • 8
2
votes
1 answer

Java 8 groupingBy to obtain LinkedHashMap and mapping the values of the map to a different object

I have this method which returns a Map: public Map> getAccountsByGroupNameMap(final List accountManagementList) { return new…
elena A
  • 35
  • 1
  • 6
2
votes
2 answers

GroupingBy with List as a result

We have the following: public List mapToBalancesWithSumAmounts(List entries) { return entries .stream() .collect( groupingBy( MonthlyBalancedBooking::getValidFor, …
2
votes
2 answers

split a list into sublists by grouping **only** repeated elements that are adjacent to each other

Is there a simple way to split a list into sublists by grouping only repeated elements that are adjacent to each other? Simple example with the folowing list of strings: Input: [RED,RED,BLUE,BLUE,BLUE,GREEN,BLUE,BLUE,RED,RED] Output: …
wannaBeDev
  • 516
  • 3
  • 14
1
vote
1 answer

Java8-stream complex groupingBy

I want to do groupingBy and create a map that return me > my model is as below public class Partition { private String tableName; private int vendorKey; private int retailerKey; private int periodKey; private…
Shalaj
  • 579
  • 8
  • 19
1
vote
1 answer

Can I add element while using Java stream groupingby

For loop code is this. Param : ArrayList userList Map map = new HashMap(); for (User user : userList) { String[] arr = user.getStringSeq().split(DELIMITER); String key = String.join(DELIMITER, arr[MENU_IDX],…
1
vote
1 answer

Java-Stream - Group by Array 4 elements and add up threir last elements using Java 8

I have a list of string arrays List which looks like this: List dataLines = List.of( new String[]{"2002", "BRBTSS", "BRSTNCNTF212", "BRL", "12670012.4055", "84M", "-101.87", "0"}, new String[]{"2002", "BRBTSS",…
1
vote
2 answers

Java-Stream - How to apply sorting while using Collector groupingBy

I'm using Collectors.groupingBy to group may objects into list. Examples are as follows: public class Test { public static void main(String[] args) { Student student1 = new Student(98,"man","CHEN",20); Student student2 = new…
jane
  • 61
  • 5
1
vote
2 answers

Java-Stream - How Collecor groupingBy() to split a Map into multiple Submaps

I have the following Student object: public class Student { private String gradeAndClass; private String gender; private String name; // getters, constructor, etc. } In the original code, properties gradeAndClass and gender are…
guiqin
  • 53
  • 4
1
vote
2 answers

Creating Map of Maps using a custom Object as a reduction type in Java 8

I have a List class MyObject { String loanType; String loanCurrency; BigDecimal amountPaid; BigDecimal amountRemaining; } And I need to convert this list into a map Map. I've created a…
jasmeet
  • 37
  • 4
1
vote
1 answer

Java Stream - groupingBy() and counting() when a curtain Condition is met

Given the following class Test class Test { String testName; String studName; String status; } and a list of tests List tests = List.of( new Test("English", "John", "passed"), new Test("English", "Dave",…
bbKing
  • 179
  • 1
  • 8