How do you add two lists in Java?

JavaObject Oriented ProgrammingProgramming

We can use addAll() methods of List to add two lists.

Use addAll() method without index parameter

boolean addAll(Collection<? extends E> c)

Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation). The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.).

Parameters

  • c − Collection containing elements to be added to this list.

Returns

True if this list changed as a result of the call.

Throws

  • UnsupportedOperationException − If the addAll operation is not supported by this list.

  • ClassCastException − If the class of an element of the specified collection prevents it from being added to this list.

  • NullPointerException − If the specified collection contains one or more null elements and this list does not permit null elements, or if the specified collection is null.

  • IllegalArgumentException − If some property of an element of the specified collection prevents it from being added to this list.

Use addAll() method with index parameter

boolean addAll(int index, Collection<? extends E> c)

Inserts all of the elements in the specified collection into this list at the specified position (optional operation). Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in this list in the order that they are returned by the specified collection's iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.)

Parameters

  • index − Index at which to insert the first element from the specified collection.

  • c − Collection containing elements to be added to this list.

Returns

True if this list changed as a result of the call.

Throws

  • UnsupportedOperationException − If the addAll operation is not supported by this list.

  • ClassCastException − If the class of an element of the specified collection prevents it from being added to this list.

  • NullPointerException − If the specified collection contains one or more null elements and this list does not permit null elements, or if the specified collection is null.

  • IllegalArgumentException − If some property of an element of the specified collection prevents it from being added to this list.

  • IndexOutOfBoundsException − If the index is out of range (index < 0 || index > size()).

Example

Following is the example showing the usage of addAll() methods to add two lists −

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.List;

public class CollectionsDemo {
   public static void main(String[] args) {
      List<String> list = new ArrayList<>();
      list.add("A");
      list.add("B");
      list.add("C");
      System.out.println("List: " + list);
      List<String> list1 = new ArrayList<>();
      list1.add("D");
      list1.add("E");
      list1.add("F");
      System.out.println("List1: " + list1);
      list.addAll(list1);
      System.out.println("Updated List: " + list);
      List<String> list2 = new ArrayList<>();
      list2.add("G");
      list2.add("H");
      list2.add("I");
      list2.addAll(0, list);
      System.out.println("List2: " + list2);
   }
}

Output

This will produce the following result −

List: [A, B, C]
List1: [D, E, F]
Updated List: [A, B, C, D, E, F]
List2: [A, B, C, D, E, F, G, H, I]
raja
Updated on 10-May-2022 07:24:24

Advertisements