I have a List something like this:
List<String> name = [[tom,john,peter,richered][manoj,satya,sachin,andrew]]
How would I convert it into a single ArrayList, using stream
I have a List something like this:
List<String> name = [[tom,john,peter,richered][manoj,satya,sachin,andrew]]
How would I convert it into a single ArrayList, using stream
You can use flatMap in Java8,
List<List<String>> main = new ArrayList<>();
List<String> l1 = Arrays.asList("1", "2");
List<String> l2 = Arrays.asList("3", "4");
main.add(l1);
main.add(l2);
List<String> ans = main.stream()
.flatMap(list -> list.stream())
.collect(Collectors.toList());
System.out.println(ans);
OUTPUT
[1, 2, 3, 4]
ans.Use flatMap:
List<List<String>> name = Lists.newArrayList(Lists.newArrayList("a", "b", "c", "d"), Lists.newArrayList("e", "f", "g"));
List<String> namesTogether = name.stream().flatMap(Collection::stream).collect(Collectors.toList());
System.out.println(namesTogether);
[a, b, c, d, e, f, g]