Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,011 questions

51,958 answers

573 users

How to join two lists in Java

2 Answers

0 votes
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
public class MyClass {
    public static void main(String args[]) {
        List<String> list1 = Arrays.asList("java", "c", "c++", "python");
        List<String> list2 = Arrays.asList("c#", "go", "php");
         
        List<String> list3 = new ArrayList<>(list1);
        list3.addAll(list2);
        
        System.out.println(list3); 
    }
}
 
 
 
 
/*
run:
 
[java, c, c++, python, c#, go, php]
 
*/

 



answered Nov 26, 2023 by avibootz
0 votes
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.Arrays;
import java.util.List;
 
public class MyClass {
    public static void main(String args[]) {
        List<String> list1 = Arrays.asList("java", "c", "c++", "python");
        List<String> list2 = Arrays.asList("c#", "go", "php");
         
        List<String> list3 = Stream.concat(list1.stream(), list2.stream())
                                            .collect(Collectors.toList());
        System.out.println(list3); 
    }
}
 
 
 
 
/*
run:
 
[java, c, c++, python, c#, go, php]
 
*/

 



answered Nov 26, 2023 by avibootz

Related questions

1 answer 93 views
93 views asked Oct 14, 2022 by avibootz
1 answer 132 views
132 views asked Jul 5, 2021 by avibootz
1 answer 89 views
1 answer 86 views
86 views asked Mar 12, 2023 by avibootz
1 answer 128 views
128 views asked Mar 12, 2023 by avibootz
1 answer 129 views
129 views asked Aug 22, 2021 by avibootz
1 answer 115 views
115 views asked Jul 5, 2021 by avibootz
...