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 list = new ArrayList();
	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 uniqueSet = new HashSet(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

6 thoughts on “How to count duplicated items in Java List

  1. plz help me sir,

    i need to count the anti mirror itemsets same as above problem.
    for example
    t1 11
    t2 00
    t3 10
    t4 01
    t5 11

    in this example the anti mirror itemsets considered as one temsets

    itemsets original counter antimirrorcounter

    11 2 1
    00 1 1

    1. first of tell what does “anti mirror” mean and which value you want to check duplicated.. because in your sample “t1 11” is look like two values one is string and other is number.

      1. anti mirror means opposite bit. for example anti mirror of 1 is 0 and anti mirror of 0 is 1.

        dont considered about t1, t2 . . . .. .
        in the above example t1 having (11) and anti mirror of t1 is t2(00). so these two transactions wants to be treated as one transaction by incrementing the counter value….

        please i need code for this logic

        1. Try in that way

          List list = new ArrayList();

          String data=”t1 00″;

          String [] antiMirror=data.split(” “);

          list.add(antiMirror[1]);

          list.add(antiMirror[1]);

          list.add(antiMirror[1]);

          System.out.println(“nExample 1 – Count ‘a’ with frequency”);
          System.out.println(“a : ” + Collections.frequency(list, “00”));

Leave a Reply

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