AbstractSequentialList in Java with Examples
Last Updated :
25 Nov, 2024
The AbstractSequentialList class in Java is a part of the Java Collection Framework and implements the Collection interface and the AbstractCollection class. This class provides a skeletal implementation of the List interface to minimize the effort required to implement this interface backed by a “sequential access” data store.
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.
Example:
AbstractSequentialList is an abstract class, so it should be assigned an instance of its subclass such as LinkedList.
Java
// Java code to illustrate AbstractSequentialList
import java.util.AbstractSequentialList;
import java.util.LinkedList;
public class GfG {
public static void main(String[] args)
{
// Creating an instance of AbstractSequentialList
AbstractSequentialList<Integer> a = new LinkedList<>();
// adding elements
a.add(5);
a.add(6);
a.add(7);
// Printing the list
System.out.println(a);
}
}
Class Hierarchy:

Declaration of AbstractSequentialList
public abstract class AbstractSequentialList<E>
extends AbstractList<E>
Where E is the type of element maintained by this List.
It implements Iterable<E>, Collection<E>, List<E> interfaces. LinkedList is the only direct subclass of AbstractSequentialList.
Constructor: protected AbstractSequentialList() – The default constructor, but being protected, it doesn’t allow to create an AbstractSequentialList object.
AbstractSequentialList<E> asl = new LinkedList<E>();
Example of AbstractSequentialList
Below is a example of AbstractSequentialList in Java:
Java
// Java code to illustrate methods of
// AbstractSequentialList
import java.util.AbstractSequentialList;
import java.util.LinkedList;
public class AbstractSequentialListDemo {
public static void main(String args[])
{
// Creating an empty AbstractSequentialList
AbstractSequentialList<String> a = new LinkedList<String>();
// Using add() method to add elements
a.add("Geeks");
a.add("for");
a.add("Geeks");
a.add("10");
a.add("20");
// Output the list
System.out.println("AbstractSequentialList: " + a);
// Remove the head using remove()
a.remove(3);
// Print the final list
System.out.println("Final List: " + a);
// Fetching the specific element from the list
// using get() method
System.out.println("The element is: " + a.get(2));
}
}
OutputAbstractSequentialList: [Geeks, for, Geeks, 10, 20]
Final List: [Geeks, for, Geeks, 20]
The element is: Geeks
Methods of AbstractSequentialList
METHOD
| DESCRIPTION
|
|---|
| add(int index, E element) | Inserts the specified element at the specified position in this list (optional operation). |
| addAll(int index, Collection<? extends E> c) | Inserts all of the elements in the specified collection into this list at the specified position (optional operation). |
| get(int index) | Returns the element at the specified position in this list. |
| iterator() | Returns an iterator over the elements in this list (in proper sequence). |
| listIterator(int index) | Returns a list iterator over the elements in this list (in proper sequence). |
| remove(int index) | Removes the element at the specified position in this list (optional operation). |
| set(int index, E element) | Replaces the element at the specified position in this list with the specified element (optional operation). |
Methods Inherited From class java.util.AbstractList
METHOD
| DESCRIPTION
|
|---|
| add(E e) | Appends the specified element to the end of this list (optional operation). |
| clear() | Removes all of the elements from this list (optional operation). |
| equals(Object o) | Compares the specified object with this list for equality. |
| hashCode() | Returns the hash code value for this list. |
| 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. |
| 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. |
| listIterator() | Returns a list iterator over the elements in this list (in proper sequence). |
| removeRange(int fromIndex, int toIndex) | Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive. |
| subList(int fromIndex, int toIndex) | Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. |
Methods Inherited From class java.util.AbstractCollection
METHOD
| DESCRIPTION
|
|---|
| addAll(Collection<? extends E> c) | Adds all of the elements in the specified collection to this collection (optional operation). |
| contains(Object o) | Returns true if this collection contains the specified element. |
| containsAll(Collection<?> c) | Returns true if this collection contains all of the elements in the specified collection. |
| isEmpty() | Returns true if this collection contains no elements. |
| remove(Object o) | Removes a single instance of the specified element from this collection, if it is present (optional operation). |
| removeAll(Collection<?> c) | Removes all of this collection’s elements that are also contained in the specified collection (optional operation). |
| retainAll(Collection<?> c) | Retains only the elements in this collection that are contained in the specified collection (optional operation). |
| toArray() | Returns an array containing all of the elements in this collection. |
| toArray(T[] a) | Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. |
| toString() | Returns a string representation of this collection. |
Methods Inherited From Interface java.util.Collection
METHOD
| DESCRIPTION
|
|---|
| parallelStream() | Returns a possibly parallel Stream with this collection as its source. |
| removeIf(Predicate<? super E> filter) | Removes all of the elements of this collection that satisfy the given predicate. |
| stream() | Returns a sequential Stream with this collection as its source. |
| toArray(IntFunction<T[]> generator) | Returns an array containing all of the elements in this collection, using the provided generator function to allocate the returned array. |
Methods Inherited From Interface java.lang.Iterable
METHOD
| DESCRIPTION
|
|---|
| forEach(Consumer<? super T> action) | Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. |
Methods Inherited From Interface java.util.List
| METHOD | DESCRIPTION |
|---|
| addAll(Collection<? extends E> c) | Appends all of the elements in the specified collection to the end of this list, in the order that they are
returned by the specified collection’s iterator (optional operation).
|
| contains(Object o) | Returns true if this list contains the specified element. |
| containsAll(Collection<?> c) | Returns true if this list contains all of the elements of the specified collection. |
| isEmpty() | Returns true if this list contains no elements. |
| remove(Object o) | Removes the first occurrence of the specified element from this list, if it is present (optional operation). |
| removeAll(Collection<?> c) | Removes from this list all of its elements that are contained in the specified collection (optional operation). |
| replaceAll(UnaryOperator<E> operator) | Replaces each element of this list with the result of applying the operator to that element. |
| retainAll(Collection<?> c) | Retains only the elements in this list that are contained in the specified collection (optional operation). |
| size() | Returns the number of elements in this list. |
| sort(Comparator<? super E> c) | Sorts this list according to the order induced by the specified Comparator. |
| spliterator() | Creates a Spliterator over the elements in this list. |
| toArray() | Returns an array containing all of the elements in this list in proper sequence (from first to last element). |
| toArray(T[] a) | Returns an array containing all of the elements in this list in proper sequence (from first to last element);
the runtime type of the returned array is that of the specified array.
|
The AbstractSequentialList class in Java is a subclass of AbstractList that provides a skeletal implementation of the List interface, specifically for lists that allow sequential access to their elements. This means that elements can be accessed in a predictable order, such as first to last.
Here is an example of how you can use the AbstractSequentialList class in Java:
Java
import java.util.AbstractSequentialList;
import java.util.List;
import java.util.ListIterator;
public class MyList extends AbstractSequentialList<Integer> {
private int size;
public MyList(int size) {
this.size = size;
}
@Override
public ListIterator<Integer> listIterator(int index) {
return new ListIterator<Integer>() {
private int currentIndex = index;
@Override
public boolean hasNext() {
return currentIndex < size;
}
@Override
public Integer next() {
return currentIndex++;
}
@Override
public boolean hasPrevious() {
return currentIndex > 0;
}
@Override
public Integer previous() {
return currentIndex--;
}
@Override
public int nextIndex() {
return currentIndex + 1;
}
@Override
public int previousIndex() {
return currentIndex - 1;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
@Override
public void set(Integer integer) {
throw new UnsupportedOperationException();
}
@Override
public void add(Integer integer) {
throw new UnsupportedOperationException();
}
};
}
@Override
public int size() {
return size;
}
public static void main(String[] args) {
List<Integer> list = new MyList(5);
for (int i : list) {
System.out.println(i);
}
}
}
By extending the AbstractSequentialList class, you only need to implement the listIterator and size methods, which provides a basic implementation of a sequential list. This can save you a lot of time and code compared to implementing the List interface from scratch.
Advantages of using AbstractSequentialList in Java:
- Reduced code duplication: By using the AbstractSequentialList class as a base, you can reduce the amount of code that you need to write to implement a sequential list, since many of the common methods have already been implemented for you.
- Consistent behavior: Since the AbstractSequentialList class implements many of the methods in the List interface, you can be sure that your implementation will have consistent behavior with other sequential list implementations, like LinkedList.
Disadvantages of using AbstractSequentialList in Java:
- Limited functionality: Since the AbstractSequentialList class is an abstract class, it provides only a basic implementation of a sequential list. You may need to implement additional methods to provide the full functionality required by your application.
- Increased complexity: By extending the `AbstractSequential
Reference: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/AbstractSequentialList.html
Similar Reads
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. This class provides a skeletal implementation of the List interface to minimize the effort required to implement this interface backed by a "s
8 min read
AbstractSequentialList size() method in Java with Example
The size() method of AbstractSequentialList in Java is used to get the size for this instance of the AbstractSequentialList. It returns an integer value which is the size for this instance of the AbstractSequentialList. Syntax: public int size() Parameters: This method does not accepts any parameter
2 min read
AbstractSequentialList conatinsAll() method in Java with Example
The containsAll() method of Java AbstractSequentialList is used to check whether two Collections contain the same elements or not. It takes one collection as a parameter and returns True if all of the elements of this collection is present in the other collection. Syntax: public boolean containsAll(
2 min read
AbstractSequentialList lastIndexOf() method in Java with Example
The lastIndexOf() method of java.util.AbstractSequentialList class is used to return the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i)
2 min read
AbstractSequentialList hashCode() method in Java with Example
The hashCode() method of AbstractSequentialList in Java is used to get the hashCode value for this instance of the AbstractSequentialList. It returns an integer value which is the hashCode value for this instance of the AbstractSequentialList. Syntax: public int hashCode() Parameters: This function
2 min read
AbstractSequentialList retainAll() method in Java with Example
The retainAll() method of java.util.AbstractSequentialList class is used to retain from this list all of its elements that are contained in the specified collection. Syntax: public boolean retainAll(Collection c) Parameters: This method takes collection c as a parameter containing elements to be ret
3 min read
AbstractSequentialList subList() method in Java with Example
The subList() method of AbstractSequentialList in Java is used to get a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so non-structural c
2 min read
AbstractSequentialList toArray() method in Java with Example
The toArray() method of Java AbstractSequentialList is used to form an array of the same elements as that of the AbstractSequentialList. Basically, it copies all the element from a AbstractSequentialList to a new array. Syntax: Object[] arr = AbstractSequentialList.toArray() Parameters: The method d
2 min read
AbstractSequentialList toArray(T[]) method in Java with Example
The toArray(arr[]) method of AbstractSequentialList class in Java is used to form an array of the same elements as that of the AbstractSequentialList. It returns an array containing all of the elements in this AbstractSequentialList in the correct order; the run-time type of the returned array is th
4 min read
AbstractSequentialList contains() method in Java with Example
The contains() method of Java AbstractSequentialList is used to check whether an element is present in a Collection or not. It takes the element as a parameter and returns True if the element is present in the collection. Syntax: public boolean contains(Object element) Parameters: The parameter elem
2 min read
AbstractSequentialList toString() method in Java with Example
The toString() method of Java AbstractSequentialList is used to return a string representation of the elements of the Collection. The String representation comprises a list representation of the elements of the Collection in the order they are picked by the iterator closed in square brackets[].This
2 min read
AbstractSequentialList clear() method in Java with Example
The clear() method of AbstractSequentialList in Java is used to remove all the elements from a list. The list will be empty after this call returns. Syntax: public void clear() Parameters: This function has no parameters. Returns: The method does not return any value. It removes all the elements in
2 min read
AbstractSequentialList indexOf() method in Java with Example
The indexOf() method of java.util.AbstractSequentialList class is used to return the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))),
2 min read
AbstractSequentialList set(int, Object) method in Java with Example
The set() method of Java AbstractSequentialList is used to replace any particular element in the list created using the AbstractSequentialList class with another element. This can be done by specifying the position of the element to be replaced and the new element in the parameter of the set() metho
3 min read
AbstractSequentialList removeAll() method in Java with Example
The removeAll() method of java.util.AbstractSequentialList class is used to remove from this list all of its elements that are contained in the specified collection. Syntax: public boolean removeAll(Collection c) Parameters: This method takes collection c as a parameter containing elements to be rem
3 min read
AbstractSequentialList isEmpty() method in Java with Example
The isEmpty() method of AbstractSequentialList in Java is used to check whether this AbstractSequentialList is empty or not. It returns an boolean value stating the same. Syntax: public boolean isEmpty() Parameters: This function has no parameters. Returns: The method returns an boolean value which
2 min read
AbstractSequentialList equals() method in Java with Example
The equals() method of java.util.AbstractSequentialList class is used to check if this AbstractSequentialList object is equal to the object passed as the parameter. This method returns a boolean value stating the same. Syntax: public boolean equals(Object o) Parameters: This method takes the object
2 min read
AbstractSequentialList set() method in Java with Examples
The set() method of Java.util.AbstractSequentialList is used to replace any particular element in the sequential list created using the LinkedList class with another element. This can be done by specifying the position of the element to be replaced and the new element in the parameter of the set() m
3 min read
AbstractSequentialList get() method in Java with Examples
The get() method of AbstractSequentialList is used to fetch or retrieve an element at a specific index from a AbstractSequentialList. Syntax: AbstractSequentialList.get(int index) Parameters: The parameter index is of integer data type that specifies the position or index of the element to be fetche
2 min read
AbstractSequentialList remove() method in Java with Examples
The remove(int index) method of AbstractSequentialList is used to remove an element from an abstract sequential list from a specific position or index. Syntax: AbstractSequentialList.remove(int index) Parameters: The parameter index is of integer data type and specifies the position of the element t
2 min read
AbstractSequentialList addAll() Method in Java with Examples
The addAll(int index, Collection C) method of AbstractSequentialList is used to append all of the elements from the collection passed as a parameter to this function at a specific index or position of a abstract sequential list. Syntax: boolean addAll(int index, Collection C) Parameters: This functi
2 min read