Created
April 8, 2019 12:54
-
-
Save developer-sdk/46dc46e04b0f1d0defc1ca90cea3833c to your computer and use it in GitHub Desktop.
자바 스트림을 이용한 맵리듀스 예제.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.ArrayList; | |
| import java.util.Map; | |
| import java.util.stream.Collectors; | |
| import java.util.stream.Stream; | |
| public class MapReduce { | |
| public static void main(String[] args) { | |
| ArrayList<String> strs = new ArrayList<>(); | |
| strs.add("Deer Bear River"); | |
| strs.add("Car Car River"); | |
| strs.add("Deer Car Bear"); | |
| // wordcount 1 | |
| Map<String, Long> map = strs.stream().flatMap(x -> Stream.of(x.split(" "))) | |
| .collect(Collectors.groupingBy(x -> x, Collectors.counting())); | |
| map.forEach((x, y) -> System.out.printf("key: %s, value: %d\n", x, y)); | |
| // wordcount 2 | |
| Map<Object, Long> map2 = strs.stream().flatMap(x -> Stream.of(x.split(" "))) | |
| .map(x -> new WordCount(x, 1)) | |
| .collect(Collectors.groupingBy(WordCount::getWord, Collectors.counting())); | |
| map2.forEach((x, y) -> System.out.printf("key: %s, value: %d\n", x, y)); | |
| } | |
| } | |
| class WordCount { | |
| public String word; | |
| public int count; | |
| public WordCount(String word, int count) { | |
| this.word = word; | |
| this.count = count; | |
| } | |
| public String getWord() { | |
| return word; | |
| } | |
| public int getCount() { | |
| return count; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment