The addAll() method of Java AbstractCollection class

Java 8Object Oriented ProgrammingProgramming

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]
raja
Published on 02-Apr-2019 11:52:21
Advertisements