Last active
May 28, 2019 15:16
-
-
Save yuizho/278fde9d8e2a6d3873c441a2ec423b99 to your computer and use it in GitHub Desktop.
This file contains 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
class StreamPlactice { | |
public static void main(String[] args) { | |
List<Bean> list = Arrays.asList( | |
new Bean(1, "a"), | |
new Bean(2, "b"), | |
new Bean(3, "c"), | |
new Bean(1, "aa"), | |
new Bean(1, "aaa"), | |
new Bean(2, "bb") | |
); | |
// collect groupingBy | |
Map<Integer, List<Bean>> map = | |
list.stream().collect(groupingBy(Bean::getKey)); | |
System.out.println(map); | |
// flat化 | |
List<String> flattenList = map.entrySet().stream() | |
.map(e -> e.getValue().stream() | |
.map(b -> b.getValue()) | |
.reduce("",(src, dest) -> src + dest) | |
).collect(toList()); | |
System.out.println(flattenList); | |
} | |
static class Bean { | |
private final int key; | |
private final String value; | |
public Bean(int key, String value) { | |
this.key = key; | |
this.value = value; | |
} | |
public int getKey() { | |
return key; | |
} | |
public String getValue() { | |
return value; | |
} | |
@Override | |
public String toString() { | |
return "Bean:{" + | |
"key=" + key + | |
", value='" + value + '\'' + | |
'}'; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment