Created
March 6, 2016 18:10
-
-
Save dodalovic/571f1730774ccae6d949 to your computer and use it in GitHub Desktop.
Flat map demo
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
package rs.dodalovic.streams.flap_map; | |
import java.util.List; | |
public class Developer { | |
private final String name; | |
private final List<String> programmingLanguages; | |
public Developer(String name, List<String> programmingLanguages) { | |
this.name = name; | |
this.programmingLanguages = programmingLanguages; | |
} | |
public String getName() { | |
return name; | |
} | |
public List<String> getProgrammingLanguages() { | |
return programmingLanguages; | |
} | |
} |
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
package rs.dodalovic.streams.flap_map; | |
import java.util.List; | |
import java.util.Set; | |
import java.util.function.Function; | |
import java.util.stream.Collectors; | |
import java.util.stream.Stream; | |
import static java.util.Arrays.asList; | |
public class FlatMapMain { | |
private static final String JAVA = "Java"; | |
private static final String PHP = "Php"; | |
private static final String JAVASCRIPT = "Javascript"; | |
private static final String RUBY = "Ruby"; | |
private static final String SCALA = "Scala"; | |
private static final String PYTHON = "Python"; | |
private static final String CLOJURE = "Clojure"; | |
public static void main(String[] args) { | |
final List<Developer> developers = asList( | |
new Developer("John", asList(JAVA, PHP)), | |
new Developer("Peter", asList(JAVA, PHP, JAVASCRIPT)), | |
new Developer("Mike", asList(JAVA, PHP, CLOJURE)), | |
new Developer("Steven", asList(JAVA, PHP, JAVASCRIPT, PYTHON)), | |
new Developer("Kylie", asList(PHP, RUBY, SCALA))); | |
final Set<String> supportedProgrammingLanguages = developers.stream() | |
.flatMap(toLanguagesStream()) | |
.collect(Collectors.toSet()); | |
System.out.format("All supported languages: \n\n"); | |
supportedProgrammingLanguages.forEach(System.out::println); | |
} | |
private static Function<Developer, Stream<String>> toLanguagesStream() { | |
return developer -> developer.getProgrammingLanguages().stream(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment