fork download
  1. import java.util.HashMap;
  2. import java.util.Map;
  3.  
  4. public class Main {
  5. public static void main(String[] args) {
  6. String text = "Hello world hello Hadoop Hadoop MapReduce world MapReduce world";
  7. String[] words = text.split("\\s+");
  8. Map<String, Integer> wordCount = new HashMap<>();
  9. for (String word : words) {
  10. word = word.toLowerCase().replaceAll("[^a-z0-9]", "");
  11. if (!word.isEmpty()) {
  12. wordCount.put(word, wordCount.getOrDefault(word, 0) + 1);
  13. }
  14. }
  15. System.out.println("=== Word Count Results ===");
  16. for (Map.Entry<String, Integer> entry : wordCount.entrySet()) {
  17. System.out.println(entry.getKey() + " : " + entry.getValue());
  18. }
  19. }
  20. }
  21.  
Success #stdin #stdout 0.2s 56140KB
stdin
mapreduce
stdout
=== Word Count Results ===
world : 3
mapreduce : 2
hello : 2
hadoop : 2