Class Declaration
The correct syntax for declaring the class for implementation of AbstractList is as follows.
Syntax
public abstract class AbstractList<E> extends AbstractCollection<E> List<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.
AbstractList is an abstract class, and it has a default constructor - 'protected AbstractList() 'hence it doesn't allow us to create an AbstractList object.
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 l List.
Code
import java.util.AbstractList;
import java.util.*;
public class List1 {
public static void main(String[] args)
{
AbstractList<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 AbstractList. 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.AbstractList;
public class AbstractListExample {
public static void main(String args[])
{
AbstractList<String> list1 = new ArrayList<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("AbstractList: "+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
AbstractList: [Hey, there!, Hope, you, are doing great]
Final List: [Hope, you, are doing great]
The element is: are doing great
Try it on Online Java Compiler.
Explanation :
Here we first imported the required packages - util and AbstractList. 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 AbstractList
-
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 the class 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.
FAQs
-
What are the packages that we need to import for abstract lists?
The two most needed packages that we need to import are Java.util and java.util.AbstractList.
-
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 List and the concept of its interfaces and its implementation. Abstract 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 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!