AbstractList in Java with Examples - GeeksforGeeks

11 Nov, 2020
The AbstractList class in Java is a part of the Java Collection Framework and implements the Collection interface and the AbstractCollection class. This class provides a skeletal implementation of the List interface to minimize the effort required to implement this interface backed by a Random Access data store (such as an array). For sequential access data (such as a linked list), AbstractSequentialList should be used in preference to this class.
To implement an unmodifiable list, for which one needs to only extend this AbstractList Class and implement the get(int) and the size() methods. To implement a modifiable list, for which one additionally override the set​(int index, E element) method (which otherwise throws an UnsupportedOperationException). If the list is variable-size, for which one should override the add(int, E) and remove(int) methods.
Class Hierarchy: 
Declaration: 
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E>

where E is the type of elements maintained by this collection.

Constructor: protected AbstractList() – The default constructor, but being protected, it doesn’t allow to create an AbstractList object.
AbstractList<E> al = new ArrayList<E>();
Example 1: AbstractList is an abstract class, so it should be assigned an instance of its subclasses such as ArrayList, LinkedList, or Vector. 
Java
// Java code to illustrate AbstractListimport java.util.*;  public class AbstractListDemo {    public static void main(String args[])    {          // Creating an empty AbstractList        AbstractList<String> list = new ArrayList<String>();          // Use add() method to add elements in the list        list.add("Geeks");        list.add("for");        list.add("Geeks");        list.add("10");        list.add("20");          // Displaying the AbstractList        System.out.println("AbstractList:" + list);    }}
  Output:
AbstractList:[Geeks, for, Geeks, 10, 20]
Example 2:
Java
// Java code to illustrate // methods of AbstractCollection   import java.util.*;   public class AbstractListDemo {     public static void main(String args[])     {           // Creating an empty AbstractList         AbstractList<String>             list = new LinkedList<String>();           // Using add() method to add elements in the list         list.add("Geeks");         list.add("for");         list.add("Geeks");         list.add("10");         list.add("20");           // Output the list         System.out.println("AbstractList: " + list);           // Remove the head using remove()         list.remove(3);           // Print the final list         System.out.println("Final AbstractList: " + list);           // getting the index of last occurence         // using lastIndexOf() method         int lastindex = list.lastIndexOf("A");           // printing the Index         System.out.println("Last index of A : "                        + lastindex);     } } 
Output:
AbstractList: [Geeks, for, Geeks, 10, 20]
Final AbstractList: [Geeks, for, Geeks, 20]
Last index of A : -1
METHOD
DESCRIPTION
addAll​(int index, 
Collection<? extends E> c)
METHOD
DESCRIPTION
METHOD
DESCRIPTION
Returns an array containing all of the elements in this collection, using the
 provided generator function to allocate the returned array.
METHOD
DESCRIPTION
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).
Returns an array containing all of the elements in this list in proper sequence (from first to last element)
; the runtime type of the returned array is that of the specified array.
Reference: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/AbstractList.html
Ganeshchowdharysadanala
Java - util package
Java-AbstractList
Java-Collections
Java
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Arrays.sort() in Java with examples
Initialize an ArrayList in Java
Reverse a string in Java
Interfaces in Java
HashMap in Java with Examples
Object Oriented Programming (OOPs) Concept in Java
ArrayList in Java