Skip to content

Commit 4b6006c

Browse files
authored
refactor: improve code and test coverage for MapReduce example (#6348)
refactor: improve code and test coverage for MapReduce example
1 parent 350f149 commit 4b6006c

File tree

2 files changed

+30
-39
lines changed

2 files changed

+30
-39
lines changed

src/main/java/com/thealgorithms/misc/MapReduce.java

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,34 @@
77
import java.util.function.Function;
88
import java.util.stream.Collectors;
99

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-
10+
/**
11+
* MapReduce is a programming model for processing and generating large data sets
12+
* using a parallel, distributed algorithm on a cluster.
13+
* It consists of two main phases:
14+
* - Map: the input data is split into smaller chunks and processed in parallel.
15+
* - Reduce: the results from the Map phase are aggregated to produce the final output.
16+
*
17+
* See also: https://en.wikipedia.org/wiki/MapReduce
18+
*/
1819
public final class MapReduce {
20+
1921
private MapReduce() {
2022
}
21-
/*
22-
*Counting all the words frequency within a sentence.
23-
*/
24-
public static String mapreduce(String sentence) {
25-
List<String> wordList = Arrays.stream(sentence.split(" ")).toList();
2623

27-
// Map step
28-
Map<String, Long> wordCounts = wordList.stream().collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()));
29-
30-
// Reduce step
31-
StringBuilder result = new StringBuilder();
32-
wordCounts.forEach((word, count) -> result.append(word).append(": ").append(count).append(","));
24+
/**
25+
* Counts the frequency of each word in a given sentence using a simple MapReduce-style approach.
26+
*
27+
* @param sentence the input sentence
28+
* @return a string representing word frequencies in the format "word: count,word: count,..."
29+
*/
30+
public static String countWordFrequencies(String sentence) {
31+
// Map phase: split the sentence into words
32+
List<String> words = Arrays.asList(sentence.trim().split("\\s+"));
3333

34-
// Removing the last ',' if it exists
35-
if (!result.isEmpty()) {
36-
result.setLength(result.length() - 1);
37-
}
34+
// Group and count occurrences of each word, maintain insertion order
35+
Map<String, Long> wordCounts = words.stream().collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()));
3836

39-
return result.toString();
37+
// Reduce phase: format the result
38+
return wordCounts.entrySet().stream().map(entry -> entry.getKey() + ": " + entry.getValue()).collect(Collectors.joining(","));
4039
}
4140
}

src/test/java/com/thealgorithms/misc/MapReduceTest.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,14 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5-
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.CsvSource;
67

78
public class MapReduceTest {
8-
@Test
9-
public void testMapReduceWithSingleWordSentence() {
10-
String oneWordSentence = "Hactober";
11-
String result = MapReduce.mapreduce(oneWordSentence);
12-
13-
assertEquals("Hactober: 1", result);
14-
}
15-
16-
@Test
17-
public void testMapReduceWithMultipleWordSentence() {
18-
String multipleWordSentence = "I Love Love HactoberFest";
19-
String result = MapReduce.mapreduce(multipleWordSentence);
20-
21-
assertEquals("I: 1,Love: 2,HactoberFest: 1", result);
9+
@ParameterizedTest
10+
@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'"})
11+
void testCountWordFrequencies(String input, String expected) {
12+
String result = MapReduce.countWordFrequencies(input);
13+
assertEquals(expected, result);
2214
}
2315
}

0 commit comments

Comments
 (0)