Repository: TheAlgorithms/Java
Pull request #4828 (2023-10-17 14:36:58)

Added MapReduce Algorithm in Misc Folder.
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

BamaCharanChhandogi: **Hello @aakiliqbal,**
You have to create another function for this operation. Next, remove the main method and then add test cases.
You can understand this:
[https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/ArrayCombination.java](url)
[https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/ArrayCombinationTest.java](url)
aakiliqbal: > **Hello @aakiliqbal,** You have to create another function for this operation. Next, remove the main method and then add test cases. You can understand this: [https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/ArrayCombination.java](url) [https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/ArrayCombinationTest.java](url)

Hey @BamaCharanChhandogi Thanks for the reply, Ok Sure.
aakiliqbal: Hey, @BamaCharanChhandogi Can you review my code? I have removed the main method and put my main code in MapReduce function along with some test cases in MapReduceTest.

Patch:
diff --git a/src/main/java/com/thealgorithms/misc/MapReduce.java b/src/main/java/com/thealgorithms/misc/MapReduce.java
new file mode 100644
index 00000000..baf960f8
--- /dev/null
+++ b/src/main/java/com/thealgorithms/misc/MapReduce.java
@@ -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();
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/misc/MapReduceTest.java b/src/test/java/com/thealgorithms/misc/MapReduceTest.java
new file mode 100644
index 00000000..213acad9
--- /dev/null
+++ b/src/test/java/com/thealgorithms/misc/MapReduceTest.java
@@ -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);
+    }
+}