fork download
  1. import java.util.*;
  2.  
  3. public class Main {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. String text = "hello hadoop hello world hadoop";
  8.  
  9. String[] words = text.split(" ");
  10.  
  11. HashMap<String, Integer> count = new HashMap<>();
  12.  
  13. // Mapper + Reducer logic
  14. for (String word : words) {
  15. if (count.containsKey(word)) {
  16. count.put(word, count.get(word) + 1);
  17. } else {
  18. count.put(word, 1);
  19. }
  20. }
  21.  
  22. // Output
  23. for (String word : count.keySet()) {
  24. System.out.println(word + " : " + count.get(word));
  25. }
  26. }
  27. }
  28.  
Success #stdin #stdout 0.11s 55556KB
stdin
Standard input is empty
stdout
world : 1
hello : 2
hadoop : 2