How to find only duplicate elements with its count from the String ArrayList in Java8?

This program finds only the duplicate elements along with their counts from a list of Strings using Stream functions.

Code Example:

public class TestNotes {

    public static void main(String[] args) {

        List<String> names = Arrays.asList("AA", "BB", "AA", "CC");

        Map<String, Long> namesCount = names.stream()
                .filter(x -> Collections.frequency(names, x) > 1)
                .collect(Collectors.groupingBy(
                        Function.identity(),
                        Collectors.counting()));

        System.out.println(namesCount);
    }
}

Output:

{AA=2}