AbstractSequentialList clear() method in Java with Example

The Java java.util package contains the AbstractSequentialList class, which offers a fundamental implementation of the List interface in order to reduce the tasks involved in implementing this interface using a "sequential access" data storage (such a linked list).

To get rid of every element from a list in Java, utilize the clear() method of the AbstractSequentialList class. Following the return of this call, the list will be empty.

Syntax:

Parameters: There are no parameters for this function.

Returns: There is no value returned by this method. It makes the list empty by removing every item from it.

Example 1:

An example implementation of LinkedList using AbstractSequentialList is shown in the Java code that is given. It uses the add method to initialize an AbstractSequentialList called a and fills it with integer values. The values from 10 to 90 are printed in the very initial list. The empty list is then produced to show how the clear method properly empties the list after the clear method has been referred to remove all of the elements from the list.

Implementation:

FileName: sequentialListExample1.java

Output:

 
The list that was created initially is: [10, 20, 30, 40, 50, 60, 70, 80, 90]
The list after using the clear() method is: []   

Example 2:

A list of programming languages can be managed using an AbstractSequentialList implemented using a LinkedList, as seen in the Java code that is provided. It initializes the list and uses the add method to fill it with strings that represent various programming languages. The first list is printed and includes languages like "JAVA", "C", "PYTHON", "HTML", "REACTJS", "C ++", and so on. Then, to demonstrate how the clear method clears the list of all of its elements, the clear method is called to remove every element from the list. The resultant empty list is then printed.

Implementation:

FileName: sequentialListExample2.java

Output:

 
The list that was created initially is: [JAVA, C, C ++, PYTHON, HTML, REACTJS, JAVASCRIPT, NODEJS, GOLANG]
The list after using the clear() method is: []