Skip to content
CodeKruPro

Count each character with a stream

QuizMediumJava onlyAsked in: Accenture, Capgemini, TCS

You need a map from each character of a word to how often it appears. A collector is the final step of a stream that gathers the elements into a result. Which collector does it in one step, given a Stream<Character>?

  • Collectors.groupingBy(Function.identity(), Collectors.counting())
  • Collectors.toMap(c -> c, c -> 1)
  • Collectors.toSet()
  • Collectors.counting()

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in