#86 Counting Word Occurrences in a List of Strings
Write a method that accepts a list of strings and counts how many times each word or phrase occurs in the list. Ignore spaces and letter case.
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
public class WordCounter {
public static void main(String[] args) {
// Create a list of strings
List<String> list = Arrays.asList(
"Hello world",
"hello World",
"HELLO WORLD!",
" Java is the best language",
"Java is the best language"
);
Map<String, Long> wordCountMap = countWordOccurrences(list);
wordCountMap.forEach((word, count) -> System.out.println(word + " " + count));
}
public static Map<String, Long> countWordOccurrences(List<String> list) {
return list.stream()
//your code
}
}
Question review
Sign in to start the dialogue
The mentor keeps the context of the current question and your previous messages.
Sign in
Available with Premium
This is just one of the solutions available with Premium. And more:
- 250+ advanced lectures
- 1300+ practice exercises
- 500+ solutions to problems from real interviews
- Personalized Java Hub certificates
- Expanded access to the AI mentor