You can use Collectors.groupingBy
to group Lists and get Map type data of Key-List. I have put together a practical sample code.
public class Product {
private String productId;
private String productName;
private String productType;
private BigDecimal price;
private long qty;
// getter setter
Create a List with the following image using the above object.
1 productName:iPhone X productType:iPhone price:120000 qty:2
2 productName:iPhone 8 Plus productType:iPhone price:110000 qty:3
3 productName:iPhone 8 productType:iPhone price:100000 qty:1
4 productName:Galaxy S9 productType:Android price:100000 qty:4
5 productName:Galaxy S9 plus productType:Android price:110000 qty:3
6 productName:Windows phone productType:Others price:80000 qty:1
7 productName:Windows phone productType:Others price:85000 qty:2
Group single items with Collectors.groupingBy
.
//Single item grouping
Map<String, List<Product>> grpByType = prdList.stream().collect(
Collectors.groupingBy(Product::getProductType));
Execution result:
{iPhone=[productId:001 productName:iPhone X productType:iPhone price:120000 qty:2, productId:002 productName:iPhone 8 Plus productType:iPhone price:110000 qty:3, productId:003 productName:iPhone 8 productType:iPhone price:100000 qty:1],
Others=[productId:006 productName:Windows phone productType:Others price:80000 qty:1, productId:007 productName:Windows phone productType:Others price:85000 qty:2],
Android=[productId:004 productName:Galaxy S9 productType:Android price:100000 qty:4, productId:005 productName:Galaxy S9 plus productType:Android price:110000 qty:3]}
Create a composite key in advance and use that composite key when grouping.
//Create a composite key
Function<Product, String> compositeKey = prd -> {
StringBuffer sb = new StringBuffer();
sb.append(prd.getProductType()).append("-").append(prd.getProductName());
return sb.toString();
};
//Grouping with compound keys
Map<String, List<Product>> grpByComplexKeys = prdList.stream().collect(
Collectors.groupingBy(compositeKey));
Execution result:
{iPhone-iPhone 8 Plus=[productId:002 productName:iPhone 8 Plus productType:iPhone price:110000 qty:3],
Android-Galaxy S9 plus=[productId:005 productName:Galaxy S9 plus productType:Android price:110000 qty:3],
Others-Windows phone=[productId:006 productName:Windows phone productType:Others price:80000 qty:1, productId:007 productName:Windows phone productType:Others price:85000 qty:2],
Android-Galaxy S9=[productId:004 productName:Galaxy S9 productType:Android price:100000 qty:4],
iPhone-iPhone 8=[productId:003 productName:iPhone 8 productType:iPhone price:100000 qty:1],
iPhone-iPhone X=[productId:001 productName:iPhone X productType:iPhone price:120000 qty:2]}
Create a composite key class and override hashCode and equals for comparison.
public class Keys {
String productType;
String productName;
public Keys(String productType, String productName) {
this.productType = productType;
this.productName = productName;
}
@Override
public int hashCode() {
//Omitted (automatically generated from IDE)
}
@Override
public boolean equals(Object obj) {
//Omitted (automatically generated from IDE)
}
Group by the created decryption key class.
//Grouping with compound keys
Map<Keys, List<Product>> grpByComplexKeys = prdList.stream().collect(
Collectors.groupingBy(prd -> new Keys(prd.getProductType(), prd.getProductName())));
Execution result:
{Android-Galaxy S9 plus=[productId:005 productName:Galaxy S9 plus productType:Android price:110000 qty:3],
iPhone-iPhone 8 Plus=[productId:002 productName:iPhone 8 Plus productType:iPhone price:110000 qty:3],
Android-Galaxy S9=[productId:004 productName:Galaxy S9 productType:Android price:100000 qty:4],
iPhone-iPhone 8=[productId:003 productName:iPhone 8 productType:iPhone price:100000 qty:1],
iPhone-iPhone X=[productId:001 productName:iPhone X productType:iPhone price:120000 qty:2],
Others-Windows phone=[productId:006 productName:Windows phone productType:Others price:80000 qty:1, productId:007 productName:Windows phone productType:Others price:85000 qty:2]}
//Grouping twice
Map<String, Map<String, List<Product>>> grpByTypeAndGrpByName = prdList.stream().collect(
Collectors.groupingBy(Product::getProductType,
Collectors.groupingBy(Product::getProductName)));
Execution result:
{iPhone={iPhone 8=[productId:003 productName:iPhone 8 productType:iPhone price:100000 qty:1], iPhone X=[productId:001 productName:iPhone X productType:iPhone price:120000 qty:2], iPhone 8 Plus=[productId:002 productName:iPhone 8 Plus productType:iPhone price:110000 qty:3]},
Others={Windows phone=[productId:006 productName:Windows phone productType:Others price:80000 qty:1, productId:007 productName:Windows phone productType:Others price:85000 qty:2]},
Android={Galaxy S9=[productId:004 productName:Galaxy S9 productType:Android price:100000 qty:4], Galaxy S9 plus=[productId:005 productName:Galaxy S9 plus productType:Android price:110000 qty:3]}}
Find the sum of int and long with Collectors.summingInt
and Collectors.summingLong
.
//INT sum
Map<String, Long> grpByTypeSumQty = prdList.stream().collect(
Collectors.groupingBy(Product::getProductType,
Collectors.summingLong(Product::getQty)));
Execution result:
{iPhone=6, Others=3, Android=7}
Find the sum of Big Decimal with Collectors.reducing
.
//Big Decimal total
Map<Object, BigDecimal> grpByTypeSum = prdList.stream().collect(
Collectors.groupingBy(Product::getProductType,
Collectors.reducing(BigDecimal.ZERO, Product::getPrice, BigDecimal::add)));
Execution result:
{iPhone=330000, Others=165000, Android=210000}
Use Comparator.comparingInt
and Comparator.comparingLong
to find the maximum value of the group.
Map<String, Optional<Product>> grpByTypeMaxInt = prdList.stream().collect(
Collectors.groupingBy(Product::getProductType,
Collectors.maxBy(Comparator.comparingLong(Product::getQty))));
Execution result:
{iPhone=Optional[productId:002 productName:iPhone 8 Plus productType:iPhone price:110000 qty:3],
Others=Optional[productId:007 productName:Windows phone productType:Others price:85000 qty:2],
Android=Optional[productId:004 productName:Galaxy S9 productType:Android price:100000 qty:4]}
You can get the maximum value of the group with Comparator.comparing
.
Map<String, Optional<Product>> grpByTypeMaxDecimal2 = prdList.stream().collect(
Collectors.groupingBy(Product::getProductType,
Collectors.maxBy(Comparator.comparing(Product::getPrice))));
You can also use your own Comparator.
//Maximum value of Big Decimal
Map<String, Optional<Product>> grpByTypeMaxDecimal = prdList.stream().collect(
Collectors.groupingBy(Product::getProductType,
Collectors.maxBy(new Comparator<Product>() {
@Override
public int compare(Product o1, Product o2) {
return o1.getPrice().compareTo(o2.getPrice());
}
}))
);
Execution result:
{iPhone=Optional[productId:001 productName:iPhone X productType:iPhone price:120000 qty:2],
Others=Optional[productId:007 productName:Windows phone productType:Others price:85000 qty:2],
Android=Optional[productId:005 productName:Galaxy S9 plus productType:Android price:110000 qty:3]}
A calculation formula (unit price x quantity) is defined in advance, and the total value of the group is calculated using it.
//Calculation formula (unit price x quantity)
Function<Product, BigDecimal> calcFunction = prd ->
prd.getPrice().multiply(new BigDecimal(prd.getQty()));
//Aggregation
Map<String, BigDecimal> grpByTypeToSum = prdList.stream().collect(
Collectors.groupingBy(Product::getProductType,
Collectors.reducing(BigDecimal.ZERO, calcFunction, BigDecimal::add)));
Execution result:
{iPhone=670000, Others=250000, Android=730000}
Recommended Posts