Created
November 2, 2014 21:34
-
-
Save vfarcic/3cc3e318c0de395caae3 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
package com.technologyconversations.java8exercises.streams; | |
import java.util.ArrayList; | |
import java.util.List; | |
import static java.util.stream.Collectors.toList; | |
public class FlatCollection { | |
public static List<String> transform7(List<List<String>> collection) { | |
List<String> newCollection = new ArrayList<>(); | |
for (List<String> subCollection : collection) { | |
for (String value : subCollection) { | |
newCollection.add(value); | |
} | |
} | |
return newCollection; | |
} | |
public static List<String> transform(List<List<String>> collection) { | |
return collection.stream() // Convert collection to Stream | |
.flatMap(value -> value.stream()) // Replace list with stream | |
.collect(toList()); // Collect results to a new list | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment