0

I have an ArrayList of Beverages

List<Beverage>beverages= Arrays.asList(
            new Beverage("Alcoholic","Can","Budweiser",new BigDecimal(5)),
            new Beverage("Alcoholic","Can","Budweiser",new BigDecimal(10)),
            new Beverage("Alcoholic","Bottle","Budweiser",new BigDecimal(15)),
            new Beverage("Alcoholic","Bottle","Budweiser",new BigDecimal(20)),
            new Beverage("Alcoholic","Can","Tecate",new BigDecimal(5)),
            new Beverage("Alcoholic","Bottle","Tecate",new BigDecimal(10)),
            new Beverage("Alcoholic","Bottle","Tecate",new BigDecimal(16)),
            new Beverage("Non-Alcoholic","Can","Coke",new BigDecimal(10)),
            new Beverage("Non-Alcoholic","Can","Coke",new BigDecimal(10)),
            new Beverage("Non-Alcoholic","Bottle","Coke",new BigDecimal(10)),
            new Beverage("Non-Alcoholic","Can","Pepsi",new BigDecimal(10)),
            new Beverage("Non-Alcoholic","Bottle","Pepsi",new BigDecimal(10)));

My Beverage POJO is

public class Beverage {
private String type;
private String containerType;   
private String itemName;
private BigDecimal quantity;
public Beverage(String type, String containerType, String itemName, BigDecimal quantity) {
    this.type = type;
    this.containerType = containerType;
    this.itemName = itemName;
    this.quantity = quantity;
}
public Beverage() {
}

}

I would like to group my list by type, containerType and itemName and sum the quatity field using Java8 Stream collector and EntrySet.

The output should be in ArrayList as shown in the attached screenshot enter image description here

  • Here you can find some good solution about [Group by multiple field names in java 8](https://stackoverflow.com/questions/28342814/group-by-multiple-field-names-in-java-8) – Eklavya Jun 25 '20 at 05:23
  • I found the answer of shmosel in this post https://stackoverflow.com/questions/41627658/group-by-two-fields-then-summing-bigdecimal working but it only consist of 2 fields, Is there a way I can do the same but using 3 fields instead of 2? – Evangelous Glo Jun 25 '20 at 05:29

1 Answers1

1

Can you try using this,

Map<String, Map<String, Map<String, Integer>>> result = beverages.stream()
                .collect(Collectors.groupingBy(Beverage::getType,
                        Collectors.groupingBy(Beverage::getContainerType,
                                Collectors.groupingBy(Beverage::getItemName,
                        Collectors.summingInt(b -> b.getQuantity().intValue())))));

Output

    {Non-Alcoholic={
                      Can={
                         Coke=20,
                         Pepsi=10
                      },
                      Bottle={
                            Coke=10,
                            Pepsi=10
                      }
                    },
     Alcoholic={
                     Can={
                        Tecate=5,
                        Budweiser=15
                     },
                     Bottle={
                        Tecate=26,
                        Budweiser=35
                     }
               }
   }
  • Sabareesh Muralidharan, It's working, however I need a BigDecimal datatype for quantity field and you used integer instead during summazation, secondly, How can I convert the "result" into ArrayList. – Evangelous Glo Jun 25 '20 at 07:18
  • Sobre el mismo codigo agregale estas lineas si quiere sumar en bigdecimal Collectors.reducing(BigDecimal.ZERO, YourObjClass::cost, BigDecimal::add)) reemplazalo por Collectors.summingInt(b -> b.getQuantity().intValue()) – jorge jesus achulla palacios Aug 14 '23 at 23:18