Running Simplified MapReduce Task In Java On Colab
[1] Install Java JDK
!apt-get install openjdk-11-jdk
[2] Create java code
%%bash
# Create the Java file
echo 'import java.util.*; import java.io.*; public class WordFrequency { public static void main(String[] args) throws IOException { String[] words = {"flight", "bad", "love", "great", "delay", "service", "amazing", "worst", "happy", "customer"}; int[] counts = {150, 120, 100, 80, 60, 50, 40, 30, 20, 10}; Map<String, Integer> wordCountMap = new HashMap<>(); for (int i = 0; i < words.length; i++) { wordCountMap.put(words[i], counts[i]); } List<Map.Entry<String, Integer>> sortedEntries = new ArrayList<>(wordCountMap.entrySet()); sortedEntries.sort((a, b) -> b.getValue().compareTo(a.getValue())); System.out.println("Top 10 Word Frequencies:"); for (int i = 0; i < Math.min(10, sortedEntries.size()); i++) { System.out.println(sortedEntries.get(i).getKey() + ": " + sortedEntries.get(i).getValue()); } } }' > WordFrequency.java
[3] Compile and Run
%%bash
# Compile the Java program
javac WordFrequency.java
# Run the Java program
java WordFrequency
Output:
Top 10 Word Frequencies:
flight: 150
bad: 120
love: 100
great: 80
delay: 60
service: 50
amazing: 40
worst: 30
happy: 20
customer: 10