Last active
March 1, 2016 11:46
-
-
Save hacksoldier/8307b80408ec7ccfaf6c 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
/** | |
* Copyright (c) 2016 Marco Velluto | |
* @since Java 1.8 | |
* @dependency: <groupId>joda-time</groupId> | |
*/ | |
import java.util.ArrayList; | |
import java.util.HashSet; | |
import java.util.List; | |
import java.util.Set; | |
import java.util.stream.Collectors; | |
import org.junit.Assert; | |
import org.junit.Test; | |
public class FlatMapTest | |
{ | |
@Test | |
public void flatMap() | |
{ | |
List<Developer> team = new ArrayList<>(); | |
Developer polyglot = new Developer("esoteric"); | |
polyglot.add("clojure"); | |
polyglot.add("scala"); | |
polyglot.add("groovy"); | |
polyglot.add("go"); | |
Developer busy = new Developer("pragmatic"); | |
busy.add("java"); | |
busy.add("javascript"); | |
team.add(polyglot); | |
team.add(busy); | |
List<String> teamLanguages = team.stream().map(developer -> developer.getLanguages()).flatMap(list -> list.stream()) | |
.collect(Collectors.toList()); | |
Assert.assertTrue(teamLanguages.containsAll(polyglot.getLanguages())); | |
Assert.assertTrue(teamLanguages.containsAll(busy.getLanguages())); | |
} | |
private class Developer | |
{ | |
private String name; | |
private Set<String> languages; | |
public Developer(String name) | |
{ | |
this.languages = new HashSet<>(); | |
this.name = name; | |
} | |
public void add(String language) | |
{ | |
this.languages.add(language); | |
} | |
public Set<String> getLanguages() | |
{ | |
return languages; | |
} | |
public String getName() | |
{ | |
return name; | |
} | |
public void setName(String name) | |
{ | |
this.name = name; | |
} | |
public void setLanguages(Set<String> languages) | |
{ | |
this.languages = languages; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment