- Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathMapReduce.java
41 lines (34 loc) · 1.39 KB
/
MapReduce.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
packagecom.thealgorithms.misc;
importjava.util.Arrays;
importjava.util.LinkedHashMap;
importjava.util.List;
importjava.util.Map;
importjava.util.function.Function;
importjava.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
*/
publicfinalclassMapReduce {
privateMapReduce() {
}
/*
*Counting all the words frequency within a sentence.
*/
publicstaticStringmapreduce(Stringsentence) {
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
StringBuilderresult = newStringBuilder();
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);
}
returnresult.toString();
}
}