AbstractList in Java with Examples

The AbstractList class in Java is a part of the Java Collection Framework and implements the Collection interface and the AbstractCollection class. It is used to implement an unmodifiable list, for which one needs to only extend this AbstractList Class and implement only the get() and the size() methods.

Class Hierarchy:

java.lang.Object
 ↳ java.util.AbstractCollection<E>
    ↳ Class AbstractList<E>

Syntax:



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

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

Constructors in Java AbstractList:

  • protected AbstractList(): The default constructor, but being protected, it doesn’t allow to create an AbstractList object.

Below is the sample program to illustrate AbstractList in Java:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate AbstractList
  
import java.util.*;
  
public class AbstractListDemo {
    public static void main(String args[])
    {
  
        // Creating an empty AbstractList
        AbstractList<String> list = new LinkedList<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);
    }
}

chevron_right


Output:

AbstractList:[Geeks, for, Geeks, 10, 20]

Methods in Java AbstractList:

  1. add(E e): Appends the specified element to the end of this list (optional operation).
  2. add(int index, E element): Inserts the specified element at the specified position in this list (optional operation).
  3. addAll(int index, Collection c): Inserts all of the elements in the specified collection into this list at the specified position (optional operation).
  4. clear(): Removes all of the elements from this list (optional operation).
  5. equals(Object o): Compares the specified object with this list for equality.
  6. get(int index): Returns the element at the specified position in this list.
  7. hashCode(): Returns the hash code value for this list.
  8. indexOf(Object o): Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
  9. iterator(): Returns an iterator over the elements in this list in proper sequence.
  10. lastIndexOf(Object o): Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
  11. listIterator(): Returns a list iterator over the elements in this list (in proper sequence).
  12. listIterator(int index): Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
  13. remove(int index): Removes the element at the specified position in this list (optional operation).
  14. removeRange(int fromIndex, int toIndex): Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive.
  15. set(int index, E element): Replaces the element at the specified position in this list with the specified element (optional operation).
  16. subList(int fromIndex, int toIndex): Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
  17. Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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);
        }
    }

    chevron_right

    
    

    Output:

    AbstractList: [Geeks, for, Geeks, 10, 20]
    Final AbstractList: [Geeks, for, Geeks, 20]
    Last index of A : -1
    

    Reference: https://docs.oracle.com/javase/7/docs/api/java/util/AbstractList.html



    My Personal Notes arrow_drop_up

    Technical Content Engineer at GeeksForGeeks

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.