Table of contents
1.
Introduction
2.
Class Hierarchy
3.
Class Declaration
3.1.
Syntax
3.1.1.
Explanation:
3.2.
Example 1
3.2.1.
Code 
3.2.2.
Output 
3.2.3.
Explanation :
3.3.
Example 2
3.3.1.
Code 
3.3.2.
Output
3.3.3.
Explanation :
4.
Methods Inherited
4.1.
Functions of AbstractList
4.2.
Functions inherited from the class abstract list and Collection packages.
5.
FAQs
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Abstract List

Author Ayushi Poddar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Abstract List is a class type part of the Java Collection Framework that is used to implement the Collection Interface and the Abstract Collection class. In Java, a specified framework is named the “ Collection Framework, “defined in JDK 1.2. This framework holds all the collection classes and interfaces, and Abstract Sequenced List is one such example. A collection Framework is a group of individual objects represented as a single unit known as a collection of things.

The AbstractList class provides the entire model for implementation of the List interface, in order to minimize the efforts required and is backed by a Random Access data store, for example, an array. In case of sequential access of data, such as a linked list, we use AbstractSequentialList as the preferred class.

Also read, Duck Number in Java and Hashcode Method in Java

Class Hierarchy

Below is the class hierarchy that is followed for implementing Abstract Lists.

Also see,  Swap Function in Java

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

  1.  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.
  2. 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!

 

Live masterclass