Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaJava Program to Concatenate Two List

Java Program to Concatenate Two List

Concatenating two lists means merging two lists into a single list.

Consider the given lists:

LIST 1

LIST 2

LIST AFTER CONCATENATION

There are several methods to perform concatenation operation:

  1. Using addAll() method
  2. Using stream
  3. Using union()

Method 1: Using addAll() method

Syntax:

addAll ( list name ) 

This method takes name of list as argument and add all the elements of the specified list in the same order as the original list. 

  • Create a new empty list ( concatenated_list)
  • Use addAll () method to concatenate the given list1 and list2 into the newly created list.

concatenated_list.addAll (list1) // concatenates first list 

concatenated_list.addAll (list2) // concatenates second list 

After performing the above steps our empty list now contains both the list.

Java




// Java Program to Concatenate Two List
// using addAll() method
  
import java.io.*;
import java.util.ArrayList;
import java.util.List;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // given list 1
        List<Integer> list1 = new ArrayList<Integer>();
        list1.add(1);
        list1.add(2);
        list1.add(3);
        list1.add(4);
        
        // given list 2
        List<Integer> list2 = new ArrayList<Integer>();
        list2.add(5);
        list2.add(6);
        list2.add(7);
        list2.add(8);
  
        // creating new empty list
        List<Integer> concatenated_list
            = new ArrayList<Integer>();
  
        // using addAll( ) method to concatenate the lists
        concatenated_list.addAll(list1);
        concatenated_list.addAll(list2);
  
        System.out.println("list1: " + list1);
        System.out.println("list2: " + list2);
        System.out.println("Concatenated list: "
                           + concatenated_list);
    }
}


Output

list1: [1, 2, 3, 4]
list2: [5, 6, 7, 8]
Concatenated list: [1, 2, 3, 4, 5, 6, 7, 8]

Method 2: Using streams

Stream.concat(list1.stream(),list2.stream()).collect(Collectors.toList())

It takes two streams as argument and creates a concatenated stream out of them.Second list is appended to the first list. 

Java




// Java Program to Concatenate Two List 
// using streams
  
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // given list 1
        List<Integer> list1 = new ArrayList<Integer>();
        list1.add(1);
        list1.add(2);
        list1.add(3);
        list1.add(4);
        
        // given list 2
        List<Integer> list2 = new ArrayList<Integer>();
        list2.add(5);
        list2.add(6);
        list2.add(7);
        list2.add(8);
  
        // creating new empty list
        List<Integer> concatenated_list = new ArrayList<Integer>();
  
        // using Stream.concat() method to concatenate the lists
        concatenated_list = Stream.concat(list1.stream(), list2.stream())
                  .collect(Collectors.toList());
  
        System.out.println("list1: " + list1);
        System.out.println("list2: " + list2);
        System.out.println("Concatenated list: "+ concatenated_list);
        
    }
}


Output

list1: [1, 2, 3, 4]
list2: [5, 6, 7, 8]
Concatenated list: [1, 2, 3, 4, 5, 6, 7, 8]

Method 3: Using union() 

ListUtils.union( list1, list2) 

 It takes two list as argument and return new concatenated list. Second list is appended to the first list. 

Java




// Java program to concatenate two lists
// using union()
  
import java.io.*;
import java.util.*;
import java.lang.*;
import org.apache.commons.collections.ListUtils;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // given list 1
        List<Integer> list1 = new ArrayList<Integer>();
        list1.add(1);
        list1.add(2);
        list1.add(3);
        list1.add(4);
  
        // given list 2
        List<Integer> list2 = new ArrayList<Integer>();
        list2.add(5);
        list2.add(6);
        list2.add(7);
        list2.add(8);
  
        // creating new empty list
        List<Integer> concatenated_list = new ArrayList<Integer>();
  
        // using ListUtils.union() method to concatenate
        // the lists
        concatenated_list = ListUtils.union(list1, list2);
  
        System.out.println("list1: " + list1);
        System.out.println("list2: " + list2);
        System.out.println("Concatenated list: "
                           + concatenated_list);
    }
}


Output:

Prerequisites for running the above code:

RELATED ARTICLES

Most Popular

Recent Comments