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:
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
)
;
}
}
When you run above class, you will get below output:
[{India,20000}, {India,50000}]
{China=2, Nepal=1, India=2}
{China=50000, Nepal=30000, India=70000}
Reference:
https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html
20 February
[Fixed] Unable to obtain LocalDateTime from TemporalAccessor
Table of ContentsUnable to obtain LocalDateTime from TemporalAccessor : ReasonUnable to obtain LocalDateTime from TemporalAccessor : FixLocalDate’s parse() method with atStartOfDay()Use LocalDate instead of LocalDateTime In this article, we will see how to fix Unable to obtain LocalDateTime from TemporalAccessor in Java 8. Unable to obtain LocalDateTime from TemporalAccessor : Reason You will generally get […]
Read More
17 February
Convert LocalDate to Instant in Java
Table of ContentsJava LocalDate to InstantUsing toInstant() wth ZoneIdUsing toInstant() with ZoneOffset In this article, we will see how to convert LocalDate to Instant in Java. Java LocalDate to Instant Instant class provides an instantaneous point in time. When you want to convert LocalDate to Instant, you need to provide time zone. Using toInstant() wth […]
Read More
17 February
Convert Instant to LocalDate in Java
Table of ContentsUsing ofInstant method [ Java 9+]Using ZoneDateTime’s toLocalDate() [Java 8] In this article, we will see how to convert Instant to LocalDate in java. Using ofInstant method [ Java 9+] Java 9 has introduced static method ofInstant() method in LocalDate class. It takes Instant and ZoneId as input and returns LocalDate object. [crayon-650bd51ba17ab391118892/] […]
Read More
17 February
Convert String to LocalDateTime in Java
Table of ContentsJava String to LocalDateTimeConvert String to LocalDateTime with custom format In this article, we will see how to convert String to LocalDateTime in Java. LocalDateTime class was introduced in Java 8. LocalDateTime represents local date and time without timezone information. It is represented in ISO 8601 format (yyyy-MM-ddTHH:mm:ss) by default. Java String to […]
Read More
16 February
Format LocalDateTime to String in Java
Table of ContentsJava LocalDateTime To StringConvert LocalDateTime to Time Zone ISO8601 StringParse String to LocalDateTime In this article, we will see how to format LocalDateTime to String in java. Java LocalDateTime To String To format LocalDateTime to String, we can create DateTimeFormatter and pass it to LocalDateTime’s format() method. [crayon-650bd51bea34d170639109/] Here are steps: Get LocalDateTime […]
Read More
17 October
Java 8 – Find duplicate elements in Stream
Table of ContentsIntroductionUsing distinct()Using Collections.frequency()Using Collectors.toSet()Using Collectors.toMap()Using Collectors.groupingBy()Conclusion Introduction When working with a collection of elements in Java, it is very common to have duplicate elements, and Java provides different APIs that we can use to solve the problem. Java 8 Stream provides the functionality to perform aggregate operations on a collection, and one of […]
Read More