1. Overview
In this tutorial, We will learn how to modify the Map value return type for the java 8 Grouping By operation.
In the previous article, we have seen how to perform Group by operation in java 8 with count, sum actions.
After calling the Collectors.groupingby() method it returns the Map<K, V> key and value.
In some of the cases you may need to return the key type as List or Set.
2. Modify Map Value Type to List For GroupingBy Result
In the below program, we are trying to first find the count of each string from list. Returned map key type is String, value type is Long.
Next, Let us see how to convert Long to List<String> to take all the values into List instead of getting the count.
package com.javaprogramto.java8.collectors.groupby; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; public class GroupingByModifyValueType { public static void main(String[] args) { // Creating List and adding duplicate values. List<String> strings = new ArrayList<>(); strings.add("Hello"); strings.add("Ram"); strings.add("Hello"); strings.add("Sam"); strings.add("Hello"); strings.add("Yam"); strings.add("Hello"); strings.add("Raj"); strings.add("Hello"); strings.add("Raj"); // Grouping by based on the count // Map type - <String, Long> Map<String, Long> countMap = strings.stream() .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); // printing the count of each string. System.out.println("Count : " + countMap); // Grouping by - change the Map value type // Map type - <String, List<String>> Map<String, List<String>> mapValueType = strings.stream() .collect(Collectors.groupingBy(Function.identity(), Collectors.toList())); System.out.println("List group by : " + mapValueType); } }
Output:
Count : {Yam=1, Hello=5, Raj=2, Sam=1, Ram=1} List group by : {Yam=[Yam], Hello=[Hello, Hello, Hello, Hello, Hello], Raj=[Raj, Raj], Sam=[Sam], Ram=[Ram]}
3. Modify Map Value Type to Set For GroupingBy Result
From the above output we are seeing the duplicate values in the list, Next, we want to remove the duplicates from it. So, we need to instruct to the groupingBy() method use Set instead of List.
package com.javaprogramto.java8.collectors.groupby; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; public class GroupingByModifyValueTypeSet { public static void main(String[] args) { // Creating List and adding duplicate values. List<String> strings = new ArrayList<>(); strings.add("Hello"); strings.add("Ram"); strings.add("Hello"); strings.add("Sam"); strings.add("Hello"); strings.add("Yam"); strings.add("Hello"); strings.add("Raj"); strings.add("Hello"); strings.add("Raj"); // Grouping by - change the Map value type // Map type - <String, Set<String>> Map<String, Set<String>> mapValueType = strings.stream() .collect(Collectors.groupingBy(Function.identity(), Collectors.toSet())); System.out.println("Set group by : " + mapValueType); } }
Output:
Set group by : {Yam=[Yam], Hello=[Hello], Raj=[Raj], Sam=[Sam], Ram=[Ram]}
4. Modify Map value type to Custom Objects List or Set
As of now, we have shown example to work with the simple types. Now, Let us create a Trade class with name, quantity and purchased amount.
Let us get the group by stock name and its quantities from all stocks.
Trade.java
package com.javaprogramto.java8.collectors.groupby; public class Trade { private String name; private int quantity; private double price; public Trade(String name, int quantity) { this.name = name; this.quantity = quantity; } public Trade(String name, int quantity, double price) { super(); this.name = name; this.quantity = quantity; this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getQuantity() { return quantity; } public void setQuantity(int quantity) { this.quantity = quantity; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return "Stock [ " + name + ", " + quantity + ", " + price + "]"; } }
Group By with trade name and List of quantities.
Normally, we perform the count, summing, min or max but now we need to collect the a value from Trade object into List.
Below example will modify the grouping by Map value type to List<Integer>.
package com.javaprogramto.java8.collectors.groupby; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.stream.Collectors; public class GroupingByModifyCustomObjects { public static void main(String[] args) { // Creating List and adding duplicate values. List<Trade> Trades = new ArrayList<>(); Trades.add(new Trade("JP Morgan", 10)); Trades.add(new Trade("ICICI", 20)); Trades.add(new Trade("HDFC", 30)); Trades.add(new Trade("ICICI", 40)); Trades.add(new Trade("JP Morgan", 50)); Trades.add(new Trade("JP Morgan", 60)); // group by - Trade name + List<Integer> list of Quantity's Map<String, List<Integer>> noOfTradesByName = Trades .stream() .collect(Collectors .groupingBy(Trade::getName, Collectors.mapping(Trade::getQuantity, Collectors.toList()))); // printing the count of each string. System.out.println("No of stocks by name in each trade: " + noOfTradesByName); } }
Output:
No of stocks by name in each trade: {ICICI=[20, 40], HDFC=[30], JP Morgan=[10, 50, 60]}
We can change the type to Set using toSet() method also.
5. Conclusion
In this article, We have seen how to modify the Map value type for the group by result in java 8.
No comments:
Post a Comment
Please do not add any spam links in the comments section.