Split a stream if it contains multiple comma-separated values

Viewed 3745

This code will add all elements from mylist to mylist2.

How can I change it to add elements if it finds a comma, and separates them?

ArrayList<String> mylist = new ArrayList<String>();
mylist.add("test");
mylist.add("test2, test3");
ArrayList<String> mylist2 = new ArrayList<String>();

mylist.stream()
    .forEach(s -> mylist2.add(s));

mylist2.stream().forEach(System.out::println);

So, current output is:

test
test2, test3

and I need the second arraylist to contain and then print:

test
test2
test3

I know in Java 8 there are filters. Could I do something like

.filter(p -> p.contains(",")... then something to split it?

4 Answers
Related