Last active
September 17, 2017 22:51
-
-
Save thiagoh/3219a137fb8715d975f946ddb53a8201 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.List; | |
import java.util.stream.Collectors; | |
import java.util.stream.Stream; | |
public class StreamFlatMap { | |
public static void main(String[] args) { | |
List<String> list = new ArrayList<>(); | |
list.add("foo"); | |
list.add("bar"); | |
list.add("qux"); | |
list.add("baz"); | |
list.add("quux"); | |
System.out.println(list.size()); | |
list = list.stream().flatMap(text -> { | |
if (text.startsWith("qu")) { | |
return Stream.of(text, text + "_nha"); | |
} else { | |
return Stream.of(text); | |
} | |
}).collect(Collectors.toList()); | |
System.out.println(list.size()); | |
for (String text : list) { | |
System.out.println(text); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment