How to count duplicated items in Java List
A Java example to show you how to count the total number of duplicated entries in a List, using Collections.frequency
and Map
.
CountDuplicatedList.java
package com.mkyong;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class CountDuplicatedList {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("b");
list.add("c");
list.add("a");
list.add("a");
list.add("a");
System.out.println("\nExample 1 - Count 'a' with frequency");
System.out.println("a : " + Collections.frequency(list, "a"));
System.out.println("\nExample 2 - Count all with frequency");
Set<String> uniqueSet = new HashSet<String>(list);
for (String temp : uniqueSet) {
System.out.println(temp + ": " + Collections.frequency(list, temp));
}
System.out.println("\nExample 3 - Count all with Map");
Map<String, Integer> map = new HashMap<String, Integer>();
for (String temp : list) {
Integer count = map.get(temp);
map.put(temp, (count == null) ? 1 : count + 1);
}
printMap(map);
System.out.println("\nSorted Map");
Map<String, Integer> treeMap = new TreeMap<String, Integer>(map);
printMap(treeMap);
}
public static void printMap(Map<String, Integer> map){
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println("Key : " + entry.getKey() + " Value : "
+ entry.getValue());
}
}
}
Output
Example 1 - Count 'a' with frequency
a : 4
Example 2 - Count all with frequency
d: 1
b: 2
c: 2
a: 4
Example 3 - Count all with Map
Key : d Value : 1
Key : b Value : 2
Key : c Value : 2
Key : a Value : 4
Sorted Map
Key : a Value : 4
Key : b Value : 2
Key : c Value : 2
Key : d Value : 1
What if I want to do the same with Java8 stream api????
myUniqueHashSet.stream().forEach(item -> {myEmptyHashMap.put(item, Collections.frequency(myArrayList,item));})
why we use tree map after the hashmap can anybody explain ?
how to find duplicate inside object in inner object
after converting list into set, the duplicates will gone right??
yup
Thank you . Superb . very much useful .
Hi, It is the very good post. Thanks for sharing.
Collections.sort(list);
for(int i=0;i<list.size();i++)
{
int k=list.lastIndexOf(list.get(i));
System.out.println(list.get(i)+" "+(k-list.indexOf(list.get(i))+1));
i=k;
}
thanks
thanks, this is very helpful
hey how to show only the items that got the highest number/duplication ?? thanks !! 😀
while removing the duplicate element in the list using the hashmap ,
I am getting the NullPointerException at “Integer count = map.get(temp);”
I modified little bit of the code
for(String st : list)
{
if(map.containsKey(st))
{
map.put(st,map.get(st)+1 );
}else
{
map.put(st,1);
}
}
Excellent logic in foreach loop with map. Kudos!!!
Very nice example
For more information visit: http://www.java-redefined.com/2013/08/java-collections-internal-working.html
Very nice example. Many thanks 🙂
Hi,how to deal with with it if case should not be considered while counting..In this case it will
Hi, Is it possible to keep the highest value along with the count? Suppose I have a collection like this
[File1, session1,transformation1,Data, 82345]
[File1, session1,transformation1,Data, 92345]
[File2, session2,transformation1,Data, 72345]
[File2, session2,transformation1,Data, 52345]
[File2, session2,transformation2,Data, 62345]
[File2, session2,transformation2,Data, 102345]
The resultant collection would be like
[File1, session1,transformation1,Data, 92345,2]
[File2, session2,transformation1,Data, 72345,2]
[File2, session2,transformation2,Data, 102345,2]
The max based on the sorting of all the columns and their count of occurrence.
public class CountDuplicateinArrayList
{
public static void main(String[] args)
{
int temp=0;
ArrayList al= new ArrayList();
al.add(“A”);
al.add(“B”);
al.add(“A”);
al.add(“B”);
al.add(“B”);
al.add(“A”);
al.add(“B”);
al.add(“A”);
al.add(“B”);
al.add(“B”);
int a=al.size();
Set s= new TreeSet(al);
int b=a-s.size();
System.out.println(” No of Dubplicate is:”+b);
Map wordMap = new HashMap();
Iterator it=al.iterator();
while(it.hasNext())
{
String ap=(String)it.next();
if(wordMap.containsKey(ap))
{
wordMap.put(ap, wordMap.get(ap)+1);
}
else
wordMap.put(ap, temp+1);
}
Set s1=wordMap.entrySet();
Iterator it1=s1.iterator();
while(it1.hasNext())
{
Map.Entry m=(Map.Entry)it1.next();
System.out.println(m.getKey()+” Comes :”+m.getValue()+” Times”);
}
}
}
thanks for sharing, it’s work 😉
Welcome.
Exactly what I was looking for!
The Collections class is full of nice feature.
Good to know it help.
check this………..
Output
With your ArrayList example, how to calculate the duplicated items in a List?
never use Collections.frequency to calculate the duplicated collection elements, thanks for sharing.
Why
I think jssay means he is “never” use
Collections.frequency
before.why not to use collections frequency method… any disadvantages or side-effects ???
HI,I am having a arraylist ArrayList data=new ArrayList();
my arraylist data is
[{“id”:”33500010060031″,”data”:”Gudlavalleru”},{“id”:”33500300090003″,”data”:”Kukatpally”},{“id”:”33500300020020″,”data”:”Nampally”},{“id”:”33500300020020″,”data”:”Nampally”},{“id”:”33500300020020″,”data”:”Nampally”},{“id”:”33500300020020″,”data”:”Nampally”},{“id”:”33500300020020″,”data”:”Nampally”}]
Now i want to the no of occurances for location?
after setting the data in array list with duplicate values i want to check the no of occurances but its not finding out..Can you give me solution?
Set uniqueSet = new HashSet(data);
System.out.println(“unique set is…”+PlatformFactory.toJson(uniqueSet));
for (DetailsTo temp : uniqueSet) {
System.out.println(PlatformFactory.toJson(temp));
dataMap.put(temp.getData(), Collections.frequency(data,temp));
System.out.println(temp + “: ” + Collections.frequency(data, temp));
}
output is:
{“id”:”33500300020020″,”data”:”Nampally”}: 1
{“id”:”33500300090003″,”data”:”Kukatpally”}: 1
{“id”:”33500300020020″,”data”:”Nampally”}: 1
{“id”:”33500300020020″,”data”:”Nampally”}: 1
{“id”:”33500010060031″,”data”:”Gudlavalleru”}: 1
{“id”:”33500300020020″,”data”:”Nampally”}: 1
{“id”:”33500300020020″,”data”:”Nampally”}: 1