What is AbstractList Class in Java?


The AbstractList class provides an implementation of the List interface.

For an unmodifiable list

Programmer needs to extend this class and provide implementations for the get(int) and size() methods.

For a modifiable list

Programmer must override the set(int, E) method. If the list is variable-size the programmer must override the add(int, E) and remove(int) methods.

The following is the syntax:

public abstract class AbstractList<E>
extends AbstractCollection<E>
implements List<E>

To work with the AbstractList class, import the following package:

import java.util.AbstractList;

The following is an example to implement AbstractList class:

Example

 Live Demo

import java.util.LinkedList;
import java.util.AbstractList;
public class Demo {
   public static void main(String[] args) {
      AbstractList<Integer> myList = new LinkedList<Integer>();
      myList.add(50);
      myList.add(100);
      myList.add(150);
      myList.add(200);
      myList.add(250);
      myList.add(300);
      myList.add(350);
      myList.add(400);
      System.out.println("Elements in the AbstractList class = " + myList);
   }
}

Output

Elements in the AbstractList class = [50, 100, 150, 200, 250, 300, 350, 400]

Updated on: 30-Jul-2019

435 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements