Search code examples
javajava-8

Count Duplicates in Java 8 using Streams Based on Field


Am trying to count how many items are duplicated from a list of Item objects. Items are duplicates if they have the same id.

e.g.

[5, 5, 2, 4, 2]

The ids 5 and 2 both occur more than once, so answer is 2.


public class Item {

    int id;

    public Item(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }
}

public class DuplicateItems {

    public static int count(List<Item> items) {
        int count = 0;
        if (items.size() == 0) {
            return 0;
        }

        items.sort(Comparator.comparingInt(Item::getId));
        Map<Object, Long> resultMap = new HashMap<>();
        items.forEach(e -> resultMap.put(e, resultMap.getOrDefault(e, 0L) + 1L));
        System.out.println(resultMap.size());
        return count;
    }

    private static List<Items> convertToList(int[] values) {
        List<Item> items = new ArrayList<>();
        for (int num : values) {
            items.add(new Item(num));
        }
        return items;
    }

    public static void main(String[] args) {
        int[] itemsArray = {5, 5, 2, 4, 2};
        List<Item> items = convertToList(itemsArray);
        int duplicateCount = count(items);
        System.out.println("Duplicate Count: " + duplicateCount);
    }
}

When I run the program, it says this:

Duplicate Count: 5

Why is the value not 2?


Solution

  • This puts them in a map based on frequency and then counts the number of values greater than 1.

           long dups = list2.stream()
           .collect(Collectors.groupingBy(Item::getId, Collectors.counting()))
                   .values().stream().filter(i-> i > 1).count();
    
           System.out.println(dups);