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

References

  1. Collections.frequency JavaDoc
  2. How to sort a Map in Java

30 comments on “How to count duplicated items in Java List

  1. why we use tree map after the hashmap can anybody explain ?

    Reply
  2. after converting list into set, the duplicates will gone right??

    Reply
    1. myUniqueHashSet.stream().forEach(item -> {myEmptyHashMap.put(item, Collections.frequency(myArrayList,item));})

      Reply
  3. 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

    Reply
  4. 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;
    }

    Reply
  5. hey how to show only the items that got the highest number/duplication ?? thanks !! 😀

    Reply
  6. 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);
    }
    }

    Reply
  7. Excellent logic in foreach loop with map. Kudos!!!

    Reply
  8. Hi,how to deal with with it if case should not be considered while counting..In this case it will

    Reply
  9. 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.

    Reply
  10. 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”);
    }

    }
    }

    Reply
  11. Exactly what I was looking for!
    The Collections class is full of nice feature.

    Reply
  12. check this………..

     import java.util.*;
    
    class ArrayListTest{
        public static void main(String args[]){
    
        ArrayList a=new ArrayList(5);
           a.add(1);
           a.add(2);
           a.add(3);
           a.add(4);
           a.ensureCapacity(5);
           System.out.println(a.size());
           a.add(5);
           a.add(1);
           System.out.println(a.size());
           a.add(1);
           a.add(2);
           a.add(3);
           a.add(4);
           System.out.println(a.size());
           a.add(5);
           a.add(1);
       
         System.out.println(a.size());
         System.out.println(a.isEmpty());
         System.out.println(a);
         
         System.out.println(a);
         System.out.println(a.get(1));
         System.out.println(a.contains("Mohan"));
         System.out.println(a.indexOf("Mohan"));
          
         HashSet h=new HashSet(a);
         System.out.println(h);
         System.out.println(a.size()-h.size());
         //h.size();
          
        a.clear();
         
         System.out.println(a.isEmpty());
        
        }
        
    }
    

    Output

    4
    6
    10
    12
    false
    [1, 2, 3, 4, 5, 1, 1, 2, 3, 4, 5, 1]
    [1, 2, 3, 4, 5, 1, 1, 2, 3, 4, 5, 1]
    2
    false
    -1
    [1, 2, 3, 4, 5]
    7
    true
    
    Reply
  13. never use Collections.frequency to calculate the duplicated collection elements, thanks for sharing.

    Reply
      1. why not to use collections frequency method… any disadvantages or side-effects ???

        Reply

Leave a Comment

Your email address will not be published. Required fields are marked *