|
| 1 | +package com.thealgorithms.misc; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.LinkedHashMap; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.function.Function; |
| 8 | +import java.util.stream.Collectors; |
| 9 | + |
| 10 | +/* |
| 11 | +* MapReduce is a programming model for processing and generating large data sets with a parallel, |
| 12 | +distributed algorithm on a cluster. |
| 13 | +* It has two main steps: the Map step, where the data is divided into smaller chunks and processed in parallel, |
| 14 | +and the Reduce step, where the results from the Map step are combined to produce the final output. |
| 15 | +* Wikipedia link : https://en.wikipedia.org/wiki/MapReduce |
| 16 | +*/ |
| 17 | + |
| 18 | +public class MapReduce { |
| 19 | + /* |
| 20 | + *Counting all the words frequency within a sentence. |
| 21 | + */ |
| 22 | + public static String mapreduce(String sentence) { |
| 23 | + List<String> wordList = Arrays.stream(sentence.split(" ")).toList(); |
| 24 | + |
| 25 | + // Map step |
| 26 | + Map<String, Long> wordCounts = wordList.stream().collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting())); |
| 27 | + |
| 28 | + // Reduce step |
| 29 | + StringBuilder result = new StringBuilder(); |
| 30 | + wordCounts.forEach((word, count) -> result.append(word).append(": ").append(count).append(",")); |
| 31 | + |
| 32 | + // Removing the last ',' if it exists |
| 33 | + if (!result.isEmpty()) { |
| 34 | + result.setLength(result.length() - 1); |
| 35 | + } |
| 36 | + |
| 37 | + return result.toString(); |
| 38 | + } |
| 39 | +} |
0 commit comments