Idiomatic solution with Java 8 threads:
import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class SplitWordCount { public static void main(String[] args) { List<String> terms = Arrays.asList( "Coding is great", "Search Engines are great", "Google is a nice search engine"); Map<String, Integer> result = terms.parallelStream(). flatMap(s -> Arrays.asList(s.split(" ")).stream()). collect(Collectors.toConcurrentMap( w -> w.toLowerCase(), w -> 1, Integer::sum)); System.out.println(result); } }
Note that you may have to consider whether upper / lower case strings should play. This option returns the strings to lowercase and uses them as keys for the final card. Result:
{coding=1, a=1, search=2, are=1, engine=1, engines=1, is=2, google=1, great=2, nice=1}
source share