In this article, we will discuss how to find and count duplicate values in a Map or HashMap
Find & Count duplicate values in a HashMap :
We will discuss 2 different approaches –
- Using Java 8 Stream
- Before Java 8
1. Using Java 8 Stream :
- Iterate through values of Map entries using Stream and then collect it to another Map (either HashMap or LinkedHashMap or TreeMap) with,
- Key as Function Identity
- Value as Duplicate Count of Map Values
- Finally, print duplicate count more than 2 to the console
GetDuplicateCountOfMapValuesUsingJava8Stream.java
package in.bench.resources.java.map;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class GetDuplicateCountOfMapValuesUsingJava8Stream {
public static void main(String[] args) {
// 1. create HashMap object
Map<String, Double> playerPercentile = new HashMap<>();
// 1.1 add map entries
playerPercentile.put("Morgan", 99.67);
playerPercentile.put("Stewart", 90.98);
playerPercentile.put("Mike", 93.65);
playerPercentile.put("Cook", 91.67);
playerPercentile.put("Stokes", 92.35);
playerPercentile.put("Broad", 99.67);
playerPercentile.put("Pietersen", 92.35);
playerPercentile.put("Hussain", 94.89);
playerPercentile.put("Collingwood", 92.35);
playerPercentile.put("Flintoff", 93.65);
playerPercentile.put("Strauss", 91.67);
playerPercentile.put("Butler", 99.67);
// 1.2 print original Map entries
System.out.println("1. Original Map Entries :- \n");
// 1.3 print Map entries to console
playerPercentile.forEach((key, value) -> System.out.println(
"Percentile : " + value + "\t" + "Player : " + key
));
// 2. Player's percentile and its duplicate count
Map<Double, Long> playersPercentileCount = playerPercentile
.values()
.stream()
.collect(
Collectors.groupingBy(Function.identity(),
HashMap::new,
Collectors.counting())
);
// 2.1 print original Map entries
System.out.println("\n\n2. Player's percentile & duplicate count :- \n");
// 2.2 print Map entries to console
playersPercentileCount.forEach((key, value) -> System.out.println(
"Percentile : " + key + "\t" + "Count : " + value
));
// 3. print duplicate count of more than 2
System.out.println("\n\n3. Percentile count more than 2 :- \n");
playersPercentileCount
.entrySet()
.stream()
.filter(entry -> entry.getValue() > 2)
.forEach(System.out::println);
}
}
Output :
1. Original Map Entries :-
Percentile : 99.67 Player : Morgan
Percentile : 93.65 Player : Mike
Percentile : 93.65 Player : Flintoff
Percentile : 92.35 Player : Stokes
Percentile : 92.35 Player : Collingwood
Percentile : 99.67 Player : Broad
Percentile : 91.67 Player : Cook
Percentile : 94.89 Player : Hussain
Percentile : 92.35 Player : Pietersen
Percentile : 90.98 Player : Stewart
Percentile : 91.67 Player : Strauss
Percentile : 99.67 Player : Butler
2. Player's percentile & duplicate count :-
Percentile : 92.35 Count : 3
Percentile : 91.67 Count : 2
Percentile : 94.89 Count : 1
Percentile : 90.98 Count : 1
Percentile : 99.67 Count : 3
Percentile : 93.65 Count : 2
3. Percentile count more than 2 :-
92.35=3
99.67=3
1.1 Duplicate Count of more than 2 :
- If the requirement is to print duplicate count of Map values more than 2 in a single line then above example’s code can be reduced to single expression as illustrated below,
playerPercentile
.values()
.stream()
.collect(
Collectors.groupingBy(Function.identity(),
HashMap::new,
Collectors.counting())
)
.entrySet()
.stream()
.filter(entry -> entry.getValue() > 2)
.forEach(System.out::println);
2. Before Java 8 :
- Iterate through values of Map entries using enhanced for-loop and then collect it to another Map with,
- Key as Duplicate Value
- Value as Duplicate Count of Map Values
- Finally, print duplicate count more than 2 using enhanced for-loop & if-condition to the console
GetDuplicateCountOfMapValues.java
package in.bench.resources.java.map;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class GetDuplicateCountOfMapValues {
public static void main(String[] args) {
// 1. create HashMap object
Map<String, Double> playerPercentile = new HashMap<>();
// 1.1 add map entries
playerPercentile.put("Morgan", 99.67);
playerPercentile.put("Stewart", 90.98);
playerPercentile.put("Mike", 93.65);
playerPercentile.put("Cook", 91.67);
playerPercentile.put("Stokes", 92.35);
playerPercentile.put("Broad", 99.67);
playerPercentile.put("Pietersen", 92.35);
playerPercentile.put("Hussain", 94.89);
playerPercentile.put("Collingwood", 92.35);
playerPercentile.put("Flintoff", 93.65);
playerPercentile.put("Strauss", 91.67);
playerPercentile.put("Butler", 99.67);
// 1.2 print original Map entries
System.out.println("1. Original Map Entries :- \n");
// 1.3 print Map entries to console
playerPercentile.forEach((key, value) -> System.out.println(
"Percentile : " + value + "\t" + "Player : " + key
));
// 2. Player's percentile and its duplicate count
Map<Double, Integer> playersPercentileCount = new HashMap<>();
// 2.1 iterate through Map entries and get duplicate values count
for(Map.Entry<String, Double> entry : playerPercentile.entrySet()) {
if(playersPercentileCount.containsKey(entry.getValue())) {
playersPercentileCount.put(
entry.getValue(), (playersPercentileCount.get(entry.getValue()) + 1)
);
}
else {
playersPercentileCount.put(entry.getValue(), 1);
}
}
// 2.1 print original Map entries
System.out.println("\n\n2. Player's percentile & duplicate count :- \n");
// 2.2 print Map entries to console
playersPercentileCount.forEach((key, value) -> System.out.println(
"Percentile : " + key + "\t" + "Count : " + value
));
// 3. print duplicate count of more than 2
System.out.println("\n\n3. Percentile count more than 2 :- \n");
// 3.1 print to console
for(Entry<Double, Integer> entry : playersPercentileCount.entrySet()) {
if(entry.getValue() > 2) {
System.out.println(entry);
}
}
}
}
Output :
1. Original Map Entries :-
Percentile : 99.67 Player : Morgan
Percentile : 93.65 Player : Mike
Percentile : 93.65 Player : Flintoff
Percentile : 92.35 Player : Stokes
Percentile : 92.35 Player : Collingwood
Percentile : 99.67 Player : Broad
Percentile : 91.67 Player : Cook
Percentile : 94.89 Player : Hussain
Percentile : 92.35 Player : Pietersen
Percentile : 90.98 Player : Stewart
Percentile : 91.67 Player : Strauss
Percentile : 99.67 Player : Butler
2. Player's percentile & duplicate count :-
Percentile : 92.35 Count : 3
Percentile : 91.67 Count : 2
Percentile : 94.89 Count : 1
Percentile : 90.98 Count : 1
Percentile : 99.67 Count : 3
Percentile : 93.65 Count : 2
3. Percentile count more than 2 :-
92.35=3
99.67=3
Related Articles :
- Java 8 – How to find duplicate and its count in a Stream or List ?
- Java 8 – How to remove duplicates from ArrayList ?
- Java 8 – How to remove duplicates from LinkedList ?
- Java 8 – How to find duplicate and its count in an Arrays ?
- Java 8 – How to remove duplicate from Arrays ?
- Java 8 – Various ways to remove duplicate elements from Arrays
- Java 8 – How to find and count duplicate values in a Map or HashMap ?
References :
- https://docs.oracle.com/javase/8/docs/api/java/util/Map.html
- https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Map.Entry.html
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html
- https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html#identity–
Happy Coding !!
Happy Learning !!