AbstractSequentialList in Java with Examples

The AbstractSequentialList 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>
    ? java.util.AbstractList<E>
      ? java.util.AbstractSequentialList<E>

Syntax:

public abstract class AbstractSequentialList<E>
    extends AbstractList<E>

Where E is the type of element maintained
by this List.

Constructors in Java AbstractSequentialList:

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

Below is a sample program to illustrate AbstractSequentialList in Java:

filter_none

edit

play_arrow

brightness_4

// Java code to illustrate AbstractSequentialList

  

import java.util.*;

  

public class GfG {

  

    public static void main(String[] args)

    {

        // Creating an instance of the AbstractSequentialList

        AbstractSequentialList<Integer>

            absl = new LinkedList<>();

  

        // adding elements to absl

        absl.add(5);

        absl.add(6);

        absl.add(7);

  

        // Printing the list

        System.out.println(absl);

    }

}

Output:

[5, 6, 7]

Methods in Java AbstractList:

  1. add(int index, E element): Inserts the specified element at the specified position in this list (optional operation).
  2. addAll(int index, Collection c): Inserts all of the elements in the specified collection into this list at the specified position (optional operation).
  3. get(int index): Returns the element at the specified position in this list.
  4. iterator(): Returns an iterator over the elements in this list (in proper sequence).
  5. list iterator(int index): Returns a list iterator over the elements in this list (in proper sequence).
  6. remove(int index): Removes the element at the specified position in this list (optional operation).
  7. set(int index, E element): Replaces the element at the specified position in this list with the specified element (optional operation).

Example:

filter_none

edit

play_arrow

brightness_4

// Java code to illustrate

// methods of AbstractSequentialList

  

import java.util.*;

import java.util.AbstractSequentialList;

  

public class AbstractSequentialListDemo {

    public static void main(String args[])

    {

  

        // Creating an empty AbstractSequentialList

        AbstractSequentialList<String>

            absqlist = new LinkedList<String>();

  

        // Using add() method to add elements in the list

        absqlist.add("Geeks");

        absqlist.add("for");

        absqlist.add("Geeks");

        absqlist.add("10");

        absqlist.add("20");

  

        // Output the list

        System.out.println("AbstractSequentialList: "

                           + absqlist);

  

        // Remove the head using remove()

        absqlist.remove(3);

  

        // Print the final list

        System.out.println("Final List: "

                           + absqlist);

  

        // Fetching the specific element from the list

        // using get() method

        System.out.println("The element is: "

                           + absqlist.get(2));

    }

}

Output:

AbstractSequentialList: [Geeks, for, Geeks, 10, 20]
Final List: [Geeks, for, Geeks, 20]
The element is: Geeks

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

 

Related Articles

post a comment