Categories: Java

AbstractSequential List in Java

The Java Collection Framework includes the AbstractSequential List class, which 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.

AbstractSequential List Method:
MethodDescription
add(int index, E element)Inserts the specified element into this list at the specified position (optional operation).
addAll(int index, Collection c)Inserts all the elements from the specified collection at the specified position into this list (optional operation).
get(int index)The element at the specified position in this list is returned.
iterator()Iterates through the elements in this list (in proper sequence).
listIterator(int index)Returns a list iterator over the list’s elements (in proper sequence).
remove(int index)The element at the specified position in this list is removed (optional operation).
set(int index, E element)Replaces the element at the specified position in this list with the specified element (optional operation).
Example:


import java.util.*;
import java.util.AbstractSequentialList;

public class Coderz {
 public static void main(String args[])
 {

  // Creating an empty AbstractSequentialList
  AbstractSequentialList<String>
   abs = new LinkedList<String>();

  // Using add() method to
  // add elements in the list
 abs.add("Accessories");
      abs.add("Home Decor");
      abs.add("Books");
      abs.add("Stationery");
      abs.add("Appliances");
      abs.add("Toys");

  // Output the list
  System.out.println("AbstractSequentialList: "
      + abs);

  // Remove the head using remove()
  abs.remove(3);

  // Print the final list
  System.out.println("Final List: "
      + abs);

  // Fetching the specific
  // element from the list
  // using get() method
  System.out.println("The element is: "
      + abs.get(2));
 }
}
Output:
AbstractSequentialList: [Accessories, Home Decor, Books, Stationery, Appliances, Toys]
Final List: [Accessories, Home Decor, Books, Appliances, Toys]
The element is: Books

Note: also read about the Abstract List in Java

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

4 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

4 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

4 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

5 months ago

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

5 months ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

5 months ago