How to join two collections in Java

1 Answer

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

public class MyClass {
    public static void main(String args[]) {
        Collection<String> co1 = Arrays.asList("java", "c", "c++");
        Collection<String> co2 = Arrays.asList("python", "php");
    
        Collection<String> result = Stream.concat(
                        co1.stream(), co2.stream())
                        .collect(Collectors.toList());
    
        System.out.println(result);
    }
}




/*
run:

[java, c, c++, python, php]

*/

 



answered Mar 12, 2023 by avibootz

Related questions

1 answer 108 views
1 answer 108 views
1 answer 138 views
2 answers 187 views
187 views asked Nov 26, 2023 by avibootz
1 answer 174 views
174 views asked Mar 12, 2023 by avibootz
...