You are viewing a single comment's thread. Return to all comments →
Can you explain this solution please? Thanks
Reads first N words, where N is the result of sc.nextInt().
Creates map of word occurences. This collector groups all words by it's identity (the word itself becomes key) and then the number of words becomes value.
The second stream contains queries.
.map(query -> counterMap.getOrDefault(query, 0L))
Then I transform each query to the appropriate count result by accessing map of counters. If there is no such key in the map then I use 0 as the default value.
Finally, I print all values.