How to join nested collection into a string in Java

1 Answer

0 votes
import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collection;
import java.util.stream.Stream;
import java.util.stream.Collectors;

public class MyClass {
    public static void main(String args[]) {
        Collection<List<String>> co = new ArrayList<>();
                co.add(Arrays.asList("java", "c", "c++", "rust"));
                co.add(Arrays.asList("c#", "python", "php"));

        String result = co.stream().map(
                    nextList -> nextList.stream()
                    .collect(Collectors.joining(", ")))
                    .collect(Collectors.joining(" :: "));

        System.out.println(result);
    }
}
  
  
  
  
/*
run:
  
java, c, c++, rust :: c#, python, php
  
*/

 



answered Mar 12, 2023 by avibootz

Related questions

1 answer 164 views
1 answer 160 views
1 answer 165 views
165 views asked Mar 12, 2023 by avibootz
1 answer 142 views
1 answer 112 views
112 views asked Mar 12, 2023 by avibootz
...