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

play_arrow

brightness_4

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

    }

}

Output:

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

Methods in Java AbstractList:

Example:

filter_none

edit

play_arrow

brightness_4

// 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

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

  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 the 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. list iterator(): Returns a list iterator over the elements in this list (in proper sequence).
  12. list iterator(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. remove range(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.

Related Articles

post a comment