The addAll() method of AbstractList class in Java


The addAll() method of the AbstractList class is used to insert all of the elements in the specified collection into this list at the specified position.

The syntax is as follows

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

Here, the parameter index is the index at which the first element would be inserted, whereas c is the collection containing elements to be added to this list.

To work with the AbstractList class, import the following package

import java.util.AbstractList;

The following is an example to implement addAll() method of the AbstractlList class in Java

Example

 Live Demo

import java.util.ArrayList;
import java.util.AbstractList;
public class Demo {
   public static void main(String[] args) {
      AbstractList<Integer> myList = new ArrayList<Integer>();
      myList.add(75);
      myList.add(100);
      myList.add(150);
      myList.add(200);
      myList.add(250);
      myList.add(150);
      myList.add(150);
      myList.add(400);
      System.out.println("Elements in AbstractList1 = " + myList);
      AbstractList<Integer> myList2 = new ArrayList<Integer>();
      myList2.add(3);
      myList2.add(5);
      myList2.add(6);
      myList2.add(8);
      System.out.println("Elements in AbstractList2 = " + myList2);
      System.out.println("Inserting elements = " + myList.addAll(3, myList2) );
      System.out.println("Updated AbstractList after index 3 = " + myList);
   }
}

Output

Elements in AbstractList1 = [75, 100, 150, 200, 250, 150, 150, 400]
Elements in AbstractList2 = [3, 5, 6, 8]
Inserting elements = true
Updated AbstractList after index 3 = [75, 100, 150, 3, 5, 6, 8, 200, 250, 150, 150, 400]

Updated on: 30-Jul-2019

104 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements