AbstractSequential List in Java

  • November 8, 2022
  • Java
java thread class

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

Leave a Reply

Your email address will not be published. Required fields are marked *