Java lambda expression for counting unique objects

Doerad :

I have set up this stream below and cant use the method count() on .map. Only on filter. But I havent set up any filter condition. How could I do it on an array of Strings for this stream below?

I want to sort out strings given the regex in replaceAll and get the unique strings and get the number of unique strings total.

Stream newStream = Arrays.stream(arr)
                    .map(s -> s.replaceAll("[^a-z]", ""))
                    .distinct()
Naman :

If you were to count the number of distinct strings, you can do it like :

long countDistinct = Arrays.stream(arr)
        .map(s -> s.replaceAll("[^a-z]", ""))
        .distinct() // intermediate operation with unique strings as 'Stream' return type
        .count();   // terminal operation with count of such strings as return type

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=130400&siteId=1