Data Source
private static List> teams = new ArrayList<>();
private static List aTeam = new ArrayList<>();
private static List bTeam = new ArrayList<>();
static {
aTeam.add("Java");
aTeam.add("C++");
aTeam.add("PHP");
aTeam.add("Scala");
bTeam.add("Go");
bTeam.add("R");
teams.add(aTeam);
teams.add(bTeam);
}
MapDemo
@Test
public void mapDemo() {
System.out.println("=== Demo Lambda Map ===");
List> result1 = teams.stream().map(team -> {
System.out.println("Team: " + team);
return team;
}).collect(Collectors.toList());
System.out.println("Result: " + result1);
}
=== Demo Lambda Map ===
Team: [Java, C++, PHP, Scala]
Team: [Go, R]
Result: [[Java, C++, PHP, Scala], [Go, R]]
FlatMap Demo
@Test
public void flatMapDemo() {
System.out.println("=== Demo Lambda FlatMap ===");
List result2 = teams.stream().flatMap(team -> {
System.out.println("Team: " + team);
return team.stream();
}).collect(Collectors.toList());
System.out.println("Result: " + result2);
}
=== Demo Lambda FlatMap ===
Team: [Java, C++, PHP, Scala]
Team: [Go, R]
Result: [Java, C++, PHP, Scala, Go, R]
沒有留言:
張貼留言