Group by multiple field names in java 8
But what if I want to group by multiple fields? I can obviously pass some POJO in groupingBy() method after implementing equals() method in that POJO but is there any other option like I can group by more than one fields from the given POJO? E.g. here in my case, I want to group by name and age.
9 Answers 9
You have a few options here. The simplest is to chain your collectors:
Map>> map = people .collect(Collectors.groupingBy(Person::getName, Collectors.groupingBy(Person::getAge));
Then to get a list of 18 year old people called Fred you would use:
A second option is to define a class that represents the grouping. This can be inside Person. This code uses a record but it could just as easily be a class (with equals and hashCode defined) in versions of Java before JEP 359 was added:
class Person < record NameAge(String name, int age) < >public NameAge getNameAge() < return new NameAge(name, age); >>
Map> map = people.collect(Collectors.groupingBy(Person::getNameAge));
Finally if you don’t want to implement your own group record then many of the Java frameworks around have a pair class designed for this type of thing. For example: apache commons pair If you use one of these libraries then you can make the key to the map a pair of the name and age:
Map, List> map = people.collect(Collectors.groupingBy(p -> Pair.of(p.getName(), p.getAge())));
Personally I don’t really see much value in generic tuples now that records are available in the language as records display intent better and require very little code.
Function
Yes I see your point. I guess (like always) it depends how they are used. The example I gave above — using a Pair as the key of a Map — is a good example of how not to do it. I’m not too familiar with Scala — will have to start learning it as I hear good things.
Just imagine being able to declare NameAge as a one-liner: case class NameAge < val name: String; val age: Int >—and you get equals , hashCode , and toString !
You can simply create a Function and let it do the work for you, kind of functional Style!
Function> compositeKey = personRecord -> Arrays.asList(personRecord.getName(), personRecord.getAge());
Now you can use it as a map:
Map> map = people.collect(Collectors.groupingBy(compositeKey, Collectors.toList()));
I used this solution but different. Function
@bpedroso This might be dangerous because if someone has name «Martin6» and age 6 he would end up in the same group as «Martin» with age 66
Hi You can simply concatenate your groupingByKey such as
Map> peopleBySomeKey = people .collect(Collectors.groupingBy(p -> getGroupingByKey(p), Collectors.mapping((Person p) -> p, toList()))); //write getGroupingByKey() function private String getGroupingByKey(Person p)
You can use List as a classifier for many fields, but you need wrap null values into Optional:
Function classifier = (item) -> List.of( item.getFieldA(), item.getFieldB(), Optional.ofNullable(item.getFieldC()) ); Map> grouped = items.stream() .collect(Collectors.groupingBy(classifier));
The groupingBy method has the first parameter is Function where:
@param the type of the input elements
@param the type of the keys
If we replace lambda with the anonymous class in your code, we can see some kind of that:
people.stream().collect(Collectors.groupingBy(new Function() < @Override public int apply(Person person) < return person.getAge(); >>));
Just now change output parameter . In this case, for example, I used a pair class from org.apache.commons.lang3.tuple for grouping by name and age, but you may create your own class for filtering groups as you need.
people.stream().collect(Collectors.groupingBy(new Function>() < @Override public YourFilter apply(Person person) < return Pair.of(person.getAge(), person.getName()); >>));
Finally, after replacing with lambda back, code looks like that:
Map, List> peopleByAgeAndName = people.collect(Collectors.groupingBy(p -> Pair.of(person.getAge(), person.getName()), Collectors.mapping((Person p) -> p, toList())));
Define a class for key definition in your group.
class KeyObj < ArrayListkeys; public KeyObj( Object. objs ) < keys = new ArrayList(); for (int i = 0; i < objs.length; i++) < keys.add( objs[i] ); >> // Add appropriate isEqual() . you IDE should generate this >
peopleByManyParams = people .collect(Collectors.groupingBy(p -> new KeyObj( p.age, p.other1, p.other2 ), Collectors.mapping((Person p) -> p, toList())));
I needed to make report for a catering firm which serves lunches for various clients. In other words, catering may have on or more firms which take orders from catering, and it must know how many lunches it must produce every single day for all it’s clients !
Just to notice, I didn’t use sorting, in order not to over complicate this example.
@Test public void test_2() throws Exception < Firm catering = DS.firm().get(1); LocalDateTime ldtFrom = LocalDateTime.of(2017, Month.JANUARY, 1, 0, 0); LocalDateTime ldtTo = LocalDateTime.of(2017, Month.MAY, 2, 0, 0); Date dFrom = Date.from(ldtFrom.atZone(ZoneId.systemDefault()).toInstant()); Date dTo = Date.from(ldtTo.atZone(ZoneId.systemDefault()).toInstant()); ListLON = DS.firm().getAllOrders(catering, dFrom, dTo, false); Map M = LON.stream().collect( Collectors.groupingBy(p -> Arrays.asList(p.getDatum(), p.getPerson().getIdfirm(), p.getIdProduct()), Collectors.counting())); for (Map.Entry e : M.entrySet()) < Object key = e.getKey(); Long value = e.getValue(); System.err.println(String.format("Client firm :%s, total: %d", key, value)); >>
This is how I did grouping by multiple fields branchCode and prdId, Just posting it for someone in need
import java.math.BigDecimal; import java.math.BigInteger; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** * * @author charudatta.joshi */ public class Product1 < public BigInteger branchCode; public BigInteger prdId; public String accountCode; public BigDecimal actualBalance; public BigDecimal sumActBal; public BigInteger countOfAccts; public Product1() < >public Product1(BigInteger branchCode, BigInteger prdId, String accountCode, BigDecimal actualBalance) < this.branchCode = branchCode; this.prdId = prdId; this.accountCode = accountCode; this.actualBalance = actualBalance; >public BigInteger getCountOfAccts() < return countOfAccts; >public void setCountOfAccts(BigInteger countOfAccts) < this.countOfAccts = countOfAccts; >public BigDecimal getSumActBal() < return sumActBal; >public void setSumActBal(BigDecimal sumActBal) < this.sumActBal = sumActBal; >public BigInteger getBranchCode() < return branchCode; >public void setBranchCode(BigInteger branchCode) < this.branchCode = branchCode; >public BigInteger getPrdId() < return prdId; >public void setPrdId(BigInteger prdId) < this.prdId = prdId; >public String getAccountCode() < return accountCode; >public void setAccountCode(String accountCode) < this.accountCode = accountCode; >public BigDecimal getActualBalance() < return actualBalance; >public void setActualBalance(BigDecimal actualBalance) < this.actualBalance = actualBalance; >@Override public String toString() < return "Product'; > public static void main(String[] args) < Listal = new ArrayList(); System.out.println(al); al.add(new Product1(new BigInteger("01"), new BigInteger("11"), "001", new BigDecimal("10"))); al.add(new Product1(new BigInteger("01"), new BigInteger("11"), "002", new BigDecimal("10"))); al.add(new Product1(new BigInteger("01"), new BigInteger("12"), "003", new BigDecimal("10"))); al.add(new Product1(new BigInteger("01"), new BigInteger("12"), "004", new BigDecimal("10"))); al.add(new Product1(new BigInteger("01"), new BigInteger("12"), "005", new BigDecimal("10"))); al.add(new Product1(new BigInteger("01"), new BigInteger("13"), "006", new BigDecimal("10"))); al.add(new Product1(new BigInteger("02"), new BigInteger("11"), "007", new BigDecimal("10"))); al.add(new Product1(new BigInteger("02"), new BigInteger("11"), "008", new BigDecimal("10"))); al.add(new Product1(new BigInteger("02"), new BigInteger("12"), "009", new BigDecimal("10"))); al.add(new Product1(new BigInteger("02"), new BigInteger("12"), "010", new BigDecimal("10"))); al.add(new Product1(new BigInteger("02"), new BigInteger("12"), "011", new BigDecimal("10"))); al.add(new Product1(new BigInteger("02"), new BigInteger("13"), "012", new BigDecimal("10"))); //Map counting = al.stream().collect(Collectors.groupingBy(Product1::getBranchCode, Collectors.counting())); // System.out.println(counting); //group by branch code Map groupByBrCd = al.stream().collect(Collectors.groupingBy(Product1::getBranchCode, Collectors.toList())); System.out.println("\n\n\n" + groupByBrCd); Map groupByPrId = null; // Create a final List to show for output containing one element of each group List finalOutputList = new LinkedList(); Product newPrd = null; // Iterate over resultant Map Of List Iterator brItr = groupByBrCd.keySet().iterator(); Iterator prdidItr = null; BigInteger brCode = null; BigInteger prdId = null; Map tempMap = null; List accListPerBr = null; List accListPerBrPerPrd = null; Product1 tempPrd = null; Double sum = null; while (brItr.hasNext()) < brCode = brItr.next(); //get list per branch accListPerBr = groupByBrCd.get(brCode); // group by br wise product wise groupByPrId=accListPerBr.stream().collect(Collectors.groupingBy(Product1::getPrdId, Collectors.toList())); System.out.println("===================="); System.out.println(groupByPrId); prdidItr = groupByPrId.keySet().iterator(); while(prdidItr.hasNext())< prdId=prdidItr.next(); // get list per brcode+product code accListPerBrPerPrd=groupByPrId.get(prdId); newPrd = new Product(); // Extract zeroth element to put in Output List to represent this group tempPrd = accListPerBrPerPrd.get(0); newPrd.setBranchCode(tempPrd.getBranchCode()); newPrd.setPrdId(tempPrd.getPrdId()); //Set accCOunt by using size of list of our group newPrd.setCountOfAccts(BigInteger.valueOf(accListPerBrPerPrd.size())); //Sum actual balance of our of list of our group sum = accListPerBrPerPrd.stream().filter(o ->o.getActualBalance() != null).mapToDouble(o -> o.getActualBalance().doubleValue()).sum(); newPrd.setSumActBal(BigDecimal.valueOf(sum)); // Add product element in final output list finalOutputList.add(newPrd); > > System.out.println("+++++++++++++++++++++++"); System.out.println(finalOutputList); > >
+++++++++++++++++++++++ [Product, Product, Product, Product, Product, Product]
[ Product, Product, Product, Product, Product, Product ]