Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to manipulate the string using stream API in Java. which api method is suitable

How to manipulate the string using stream API in Java. which api method is suitable.

I am trying some Java 8 features.

so lets say I have list of string

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

List<String> str = Arrays.asList("Hello World","Welcome America");
        List<String> res = str.stream()
                          .map(st -> st.split(" "))
                          .collect(Collectors.)); // here after Collectors what should I use. 
                          
                      
                      

my output should be like : [Hello Welcome, Wrold America]

hwrw by using map i am transforming the list but I am not getting any method to collect that. any help so i can do it by using stream.

>Solution :

It’s not very clean or efficient, but here’s one way:

List<String> res = IntStream.range(0, str.size())
        .mapToObj(i -> str.stream()
                .map(s -> s.split(" ")[i])
                .collect(Collectors.joining(" ")))
        .collect(Collectors.toList());

Ideone Demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading