From 7f5900d855f497a2c537a5d7422abfcdeaea90a6 Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Sun, 6 Jul 2025 19:54:05 +0200 Subject: [PATCH] refactor: improve code and test coverage for MapReduce example --- .../com/thealgorithms/misc/MapReduce.java | 47 +++++++++---------- .../com/thealgorithms/misc/MapReduceTest.java | 22 +++------ 2 files changed, 30 insertions(+), 39 deletions(-) diff --git a/src/main/java/com/thealgorithms/misc/MapReduce.java b/src/main/java/com/thealgorithms/misc/MapReduce.java index c076957344f9..d98b2ee2dd03 100644 --- a/src/main/java/com/thealgorithms/misc/MapReduce.java +++ b/src/main/java/com/thealgorithms/misc/MapReduce.java @@ -7,35 +7,34 @@ 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 -*/ - +/** + * MapReduce is a programming model for processing and generating large data sets + * using a parallel, distributed algorithm on a cluster. + * It consists of two main phases: + * - Map: the input data is split into smaller chunks and processed in parallel. + * - Reduce: the results from the Map phase are aggregated to produce the final output. + * + * See also: https://en.wikipedia.org/wiki/MapReduce + */ public final class MapReduce { + private MapReduce() { } - /* - *Counting all the words frequency within a sentence. - */ - public static String mapreduce(String sentence) { - List wordList = Arrays.stream(sentence.split(" ")).toList(); - // Map step - Map 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(",")); + /** + * Counts the frequency of each word in a given sentence using a simple MapReduce-style approach. + * + * @param sentence the input sentence + * @return a string representing word frequencies in the format "word: count,word: count,..." + */ + public static String countWordFrequencies(String sentence) { + // Map phase: split the sentence into words + List words = Arrays.asList(sentence.trim().split("\\s+")); - // Removing the last ',' if it exists - if (!result.isEmpty()) { - result.setLength(result.length() - 1); - } + // Group and count occurrences of each word, maintain insertion order + Map wordCounts = words.stream().collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting())); - return result.toString(); + // Reduce phase: format the result + return wordCounts.entrySet().stream().map(entry -> entry.getKey() + ": " + entry.getValue()).collect(Collectors.joining(",")); } } diff --git a/src/test/java/com/thealgorithms/misc/MapReduceTest.java b/src/test/java/com/thealgorithms/misc/MapReduceTest.java index c79c40701cc1..748dd0a563cf 100644 --- a/src/test/java/com/thealgorithms/misc/MapReduceTest.java +++ b/src/test/java/com/thealgorithms/misc/MapReduceTest.java @@ -2,22 +2,14 @@ import static org.junit.jupiter.api.Assertions.assertEquals; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; 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); + @ParameterizedTest + @CsvSource({"'hello world', 'hello: 1,world: 1'", "'one one two', 'one: 2,two: 1'", "'a a a a', 'a: 4'", "' spaced out ', 'spaced: 1,out: 1'"}) + void testCountWordFrequencies(String input, String expected) { + String result = MapReduce.countWordFrequencies(input); + assertEquals(expected, result); } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy