The addAll() method of Java AbstractCollection class


The addAll() method of the AbstractCollection class in Java is used to add all of elements in the specified collection to this collection. It returns TRUE if the elements are successfully appended

The syntax is as follows:

public boolean addAll(Collection<? extends E> c)

Here, c is the collection containing elements that is to be added to this collection.

To work with AbstractCollection class in Java, import the following package:

import java.util.AbstractCollection;

The following is an example to implement AbstractCollection addAll() method in Java:

Example

 Live Demo

import java.util.ArrayList;
import java.util.AbstractCollection;
public class Demo {
   public static void main(String[] args) {
      AbstractCollection<Object> absCollection1 = new ArrayList<Object>();
      absCollection1.add("These");
      absCollection1.add("are");
      absCollection1.add("demo");
      absCollection1.add("elements");
      System.out.println("AbstractCollection1: " + absCollection1);
      AbstractCollection<Object> absCollection2 = new ArrayList<Object>();
      absCollection2.add("Appended");
      absCollection2.add("elements");
      System.out.println("AbstractCollection2: " + absCollection2);
      // appending
      absCollection1.addAll(absCollection2) ;
      System.out.println("Displaying elements in the AbstractCollection2: " + absCollection1);
   }
}

Output

AbstractCollection1: [These, are, demo, elements]
AbstractCollection2: [Appended, elements]
Displaying elements in the AbstractCollection2: [These, are, demo, elements, Appended, elements]

Updated on: 30-Jul-2019

72 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements