Class Declaration
The correct syntax for declaring the class for implementation of Abstract Sequential List is as follows.
Syntax:
public abstract class AbstractSequentialList<E> extends AbstractList<E>
Explanation:
Here 'E' is the type of element that is to be maintained by this list. The above declaration implements Iterable<E> , Collection<E> , List<E> interfaces. LinkedList is the only direct subclass under Abstract Sequential List.
AbstractSequentialList is an abstract class, and it has a default constructor - 'protected AbstractSequentialList() 'hence it doesn't allow us to create an AbstractSequentialList object and the syntax for the same is :
AbstractSequentialList<E> asl = new LinkedList<E>();
Constructor
“ protected AbstractSequentialList() “ is the default constructor and as it is kept protected, it doesn’t allow to create an AbstractSequentialList object.
Syntax:
AbstractSequentialList<E> asl = new LinkedList<E>();
Explanation:
The Abstract Sequential List is an abstract class, that is the reason why it should be assigned an instance of it’s subclass.
Package
In order to work with the Abstract Sequential List class in Java, we also need to import the necessary packages, which are as follows :
import java.util.AbstractSequentialList;
Example 1
Now let's take up a small example of a shortcode to understand how we need to deal with the implementation of an Abstract Sequential List.
Code :
import java.util.AbstractSequentialList;
import java.util.*;
public class List1 {
public static void main(String[] args)
{
AbstractSequentialList<Integer> list1= new LinkedList<>(); //Created an instance
list1.add(99); // Adding elements to the list
list1.add(100);
list1.add(101);
System.out.println(list1); // Printing all the elements of the list
}
}
Output :
[99, 100, 101]
Explanation :
Here we first imported the required packages - util and AbstractSequentialList. After creating the main method, we created an instance of the abstract class named 'list1'. Further, we used the predefined function' .add( ) 'to add elements to this created list, one after the other. We then printed the entire list by just typing it inside the println statement.
Example 2
Let's try out a program where we use some other predefined functions as well.
Code :
import java.util.*;
import java.util.AbstractSequentialList;
public class AbstractSequentialListExample {
public static void main(String args[])
{
AbstractSequentialList<String> list1 = new LinkedList<String>(); //Created an instance
list1.add("Hey"); // Adding elements to the list
list1.add("there !");
list1.add("Hope,");
list1.add("you");
list1.add("are doing great");
System.out.println("AbstractSequentialList: "+list1); // Printing all the elements of the list
list1.remove(0); // Removing element from the list
list1.remove(1);
System.out.println("Final List: "+ list1);
System.out.println("The element is: "+ list1.get(4)); // Accessing an element from the list
}
}
Output:
AbstractSequentialList: [Hey, there!, Hope, you, are doing great]
Final List: [Hope, you, are doing great]
The element is: are doing great
Practice by yourself on online java compiler.
Explanation :
Here we first imported the required packages - util and AbstractSequentialList. After creating the main method, we created an instance of the abstract class named 'list1', and this time, the list would contain string elements. Further, we used the predefined function' .add( ) 'to add elements to this created list, one after the other. We printed the entire list by just typing it inside the println statement. We also tried using the function 'remove( ) 'to remove elements from the list, and finally, we also used the function 'get( ) 'to access an element from the list.
Methods Inherited
Functions of Abstract Sequential List
-
add(int index, Element)
This function inserts the given element at the specified position in the list.
-
addAll(int index, Collection C)
This function inserts all the elements in the specified collection into this list at the specified position.
-
get(int index)
This function returns the element at the specified position in the list.
-
set(int index, Element)
This function replaces the element at the specified position in the list with the specified element.
Functions inherited from abstract list and Collection packages.
-
clear()
This function removes all of the elements from the list.
-
hashCode()
This function returns the hash code value for the list.
-
subList(int from, int last)
This function returns a portion of the list between the specified index position 'from' to 'last.'
-
removeAll(Collection<?>C)
This function removes all of the collection's elements that are also contained in the specified collection.
-
addAll(Collection<?extends E>c)
This function appends all of the elements in the specified collection to the end of this list.
-
size()
This function returns the size of the list.
Must Read Array of Objects in Java
FAQs
What are the packages that we need to import for abstract sequential lists?
The two most needed packages that we need to import are Java.util and java.util.AbstractSequentialList.
Why do we need a Collection Framework?
Before JDK 1.2, there were standard methods for grouping Java objects such as Arrays, Vectors, or hashtables, but none of these had a common interface, which on the contrary sequential lists have.
Conclusion
This article extensively discussed the Abstract Sequential List and the concept of its interfaces and its implementation. Abstract Sequenced List is a class type that is a part of the Java Collection Framework and which is used for the implementation of the Collection Interface and Abstract Collection class. We hope that this blog has helped you enhance your knowledge regarding Abstract Sequential Lists and if you would like to learn more, check out our articles on Coding Ninjas
Recommended Readings:
Do upvote our blog to help other ninjas grow. Happy Coding!