KFleischer
KFleischer

Reputation: 1020

Best Way / data structure to count occurrences of strings

Lets assume I have a very long list of strings. I want to count the number of occurrences of each string. I don't know how many and of what kind the strings are (means: I have no dictionary of all possible strings)

My first idea was to create a Map and to increase the integer every time I find the key again.

But this feels a bit clumsy. Is there a better way to count all occurrences of those strings?

Upvotes: 2

Views: 1762

Answers (3)

Olivier Grégoire
Olivier Grégoire

Reputation: 35427

If you really want a specific datastructure, you can always look towards Guava's Multiset:

Usage will be similar to this:

List<String> words = Arrays.asList("a b c a a".split(" "));

Multiset<String> wordCounts = words.stream()
  .collect(toCollection(HashMultiset::create));

wordCounts.count("a"); // returns 3
wordCounts.count("b"); // returns 1
wordCounts.count("z"); // returns 0, no need to handle null!

Upvotes: 0

Eugene
Eugene

Reputation: 120858

You can do it without streams too:

Map<String, Long> map = new HashMap<>();

list.forEach(x -> map.merge(x, 1L, Long::sum));

Upvotes: 1

Andy Turner
Andy Turner

Reputation: 140318

Since Java 8, the easiest way is to use streams:

Map<String, Long> counts =
    list.stream().collect(
        Collectors.groupingBy(
            Function.identity(), Collectors.counting()));

Prior to Java 8, your currently outlined approach works just fine. (And the Java 8+ way is doing basically the same thing too, just with a more concise syntax).

Upvotes: 4

Related Questions