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

Estimate π Using Monte Carlo Method

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

5 days ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

6 days ago

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

2 weeks ago

Autocomplete System Implementation

Build an autocomplete system that, given a query string s and a set of possible…

2 weeks ago

Job Scheduler Implementation

Design a job scheduler that accepts a function f and an integer n. The scheduler…

2 weeks ago

Largest Sum of Non-Adjacent Numbers

Problem Statement (Asked By Airbnb) Given a list of integers, write a function to compute…

2 weeks ago