How convert string of separate words to separate elements of list by using stream api

Roman Shmandrovskyi :

Try to studying Stream API. So, I have a list of strings like this:

118.111.97.113
119.122.122.122
122.122.122.97
122.122.122.99
122.122.122.100

I need to split it by '.' and separate numbers put as separate value to new collection. Like this:

118
111
97
113
119
...

I know about flatMap method, that can assume one value and return several, but how use it correctly, have no one idea. Can you help me or give some ideas?

Thanks!

azro :

You may do

List<String> initial;
List<String> all = initial.stream().flatMap(str -> Arrays.stream(str.split("\\.")))
                                   .collect(Collectors.toList());

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=85936&siteId=1