We have already seen some examples on Collectors in previous post. Â In this post, we are going to see Java 8 Collectors groupby example. Groupby is another feature added in java 8 and it is very much similar to SQL/Oracle.
Lets understand more with the help of example: Lets create our model class country as below:
Lets create main class in which we will use Collectors.groupBy to do group by.
When you run above class, you will get below output:
Lets understand more with the help of example: Lets create our model class country as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
package org.arpit.java2blog; public class Country{ String countryName; long population; public Country() { super(); } public Country(String countryName,long population) { super(); this.countryName = countryName; this.population=population; } public String getCountryName() { return countryName; } public void setCountryName(String countryName) { this.countryName = countryName; } public long getPopulation() { return population; } public void setPopulation(long population) { this.population = population; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((countryName == null) ? 0 : countryName.hashCode()); result = prime * result + (int) (population ^ (population >>> 32)); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Country other = (Country) obj; if (countryName == null) { if (other.countryName != null) return false; } else if (!countryName.equals(other.countryName)) return false; if (population != other.population) return false; return true; } public String toString() { return "{"+countryName+","+population+"}"; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package org.arpit.java2blog; import java.math.BigDecimal; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class Java8CollectorsGroupBy { public static void main(String[] args) { List items = Arrays.asList(new Country("India", 20000), new Country("China", 40000), new Country("Nepal", 30000), new Country("India", 50000), new Country("China", 10000)); // Group by countryName Map<String, List> groupByCountry = items.stream().collect( Collectors.groupingBy(Country::getCountryName)); System.out.println(groupByCountry.get("India")); // Group by CountryName and calculates the count Map<String, Long> counting = items.stream().collect( Collectors.groupingBy(Country::getCountryName,Collectors.counting())); // Group by countryName and sum up the population System.out.println(counting); Map<String, Long> populationCount = items.stream().collect( Collectors.groupingBy(Country::getCountryName, Collectors.summingLong(Country::getPopulation))); System.out.println(populationCount); } } |
[{India,20000}, {India,50000}]
{China=2, Nepal=1, India=2}
{China=50000, Nepal=30000, India=70000}
{China=50000, Nepal=30000, India=70000}
Reference:Â
https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.