I am using Grouping-By in Streams:
1: I want to know how can i use "Grouping-By" twice in Collect method.
2: Secondly,What is the strategy to define return types in grouping?
1:
Map<String,String> sum1 = Items.items().stream()
.collect(Collectors.groupingBy(Items::getName,
Collectors.groupingBy(Items::getName)));
error message:
1:Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types: inference variable D has incompatible equality constraints java.lang.String,java.lang.Integer at collectorsinjava.CollectorsInJava.main(CollectorsInJava.java:33)
2:
Map<String, Map<String,Integer>> sum1 = Items.items().stream()
.collect(Collectors.groupingBy(Items::getName,
Collectors.groupingBy(Items::getQuantity)));
error message:
2:Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types: inference variable K has incompatible bounds equality constraints: java.lang.String lower bounds: java.lang.Integer at collectorsinjava.CollectorsInJava.main(CollectorsInJava.java:37)
This is my list of items:
List<Items> item = Arrays.asList(new Items("A",22,new BigDecimal("23.3")),
new Items("B",33,new BigDecimal("19.99")),
new Items("C",31,new BigDecimal("23.3")),
new Items("D",22,new BigDecimal("19.99")),
new Items("B",33,new BigDecimal("23.3")),
new Items("C",31,new BigDecimal("19.99")),
new Items("D",22,new BigDecimal("23.3"))
);
can someone please suggest how I can avoid the compilation errors?