We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Reads first N words, where N is the result of sc.nextInt().
.collect(groupingBy(identity(),counting()))
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.
.forEach(System.out::println)
Finally, I print all values.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sparse Arrays
You are viewing a single comment's thread. Return to all comments →
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.
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.