mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-08 18:32:56 +08:00
Added MapReduce Algorithm in Misc Folder. (#4828)
* Added MapReduce Algorithm in Misc Folder. * Did formatting correctly * Removed main function and added MapReduceTest * format the code
This commit is contained in:
39
src/main/java/com/thealgorithms/misc/MapReduce.java
Normal file
39
src/main/java/com/thealgorithms/misc/MapReduce.java
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package com.thealgorithms.misc;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* MapReduce is a programming model for processing and generating large data sets with a parallel,
|
||||||
|
distributed algorithm on a cluster.
|
||||||
|
* It has two main steps: the Map step, where the data is divided into smaller chunks and processed in parallel,
|
||||||
|
and the Reduce step, where the results from the Map step are combined to produce the final output.
|
||||||
|
* Wikipedia link : https://en.wikipedia.org/wiki/MapReduce
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class MapReduce {
|
||||||
|
/*
|
||||||
|
*Counting all the words frequency within a sentence.
|
||||||
|
*/
|
||||||
|
public static String mapreduce(String sentence) {
|
||||||
|
List<String> wordList = Arrays.stream(sentence.split(" ")).toList();
|
||||||
|
|
||||||
|
// Map step
|
||||||
|
Map<String, Long> wordCounts = wordList.stream().collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()));
|
||||||
|
|
||||||
|
// Reduce step
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
wordCounts.forEach((word, count) -> result.append(word).append(": ").append(count).append(","));
|
||||||
|
|
||||||
|
// Removing the last ',' if it exists
|
||||||
|
if (!result.isEmpty()) {
|
||||||
|
result.setLength(result.length() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
}
|
23
src/test/java/com/thealgorithms/misc/MapReduceTest.java
Normal file
23
src/test/java/com/thealgorithms/misc/MapReduceTest.java
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package com.thealgorithms.misc;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class MapReduceTest {
|
||||||
|
@Test
|
||||||
|
public void testMapReduceWithSingleWordSentence() {
|
||||||
|
String oneWordSentence = "Hactober";
|
||||||
|
String result = MapReduce.mapreduce(oneWordSentence);
|
||||||
|
|
||||||
|
assertEquals("Hactober: 1", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMapReduceWithMultipleWordSentence() {
|
||||||
|
String multipleWordSentence = "I Love Love HactoberFest";
|
||||||
|
String result = MapReduce.mapreduce(multipleWordSentence);
|
||||||
|
|
||||||
|
assertEquals("I: 1,Love: 2,HactoberFest: 1", result);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user