Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
AbstractList add(E ele) method in Java with Examples
Next article icon

AbstractList in Java with Examples

Last Updated : 25 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The AbstractList class in Java is a part of the Java Collection Framework and implements the Collection interface and the AbstractCollection class. AbstractList class provides a skeletal implementation of the List interface to minimize the effort required to implement this interface backed by a Random Access data store.

For sequential access data (such as a linked list), AbstractSequentialList should be used in preference to this class.

  • To implement an unmodifiable list, for which one needs to only extend this AbstractList Class and implement the get(int) and the size() methods.
  • To implement a modifiable list, for which one additionally override the set​(int index, E element) method (which otherwise throws an UnsupportedOperationException).
  • If the list is variable-size, for which one should override the add(int, E) and remove(int) methods.


Class Hierarchy: 

AbstractList-in-Java


Declaration of AbstractList

public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E>

where E is the type of elements maintained by this collection.

Constructor: protected AbstractList() - The default constructor, but being protected, it doesn't allow to create an AbstractList object.

AbstractList<E> al = new ArrayList<E>();

Example 1: AbstractList is an abstract class, so it should be assigned an instance of its subclasses such as ArrayList, LinkedList, or Vector. 

Java
// Java code to illustrate AbstractList import java.util.*;  public class AbstractListDemo {     public static void main(String args[])     {          // Creating an empty AbstractList         AbstractList<String> list = new ArrayList<String>();          // Use add() method to add elements in the list         list.add("Geeks");         list.add("for");         list.add("Geeks");         list.add("10");         list.add("20");          // Displaying the AbstractList         System.out.println("AbstractList:" + list);     } } 

Output
AbstractList:[Geeks, for, Geeks, 10, 20]

Example 2:

Java
// Java code to illustrate  // methods of AbstractCollection   import java.util.*;   public class AbstractListDemo {      public static void main(String args[])      {           // Creating an empty AbstractList          AbstractList<String>              list = new LinkedList<String>();           // Using add() method to add elements in the list          list.add("Geeks");          list.add("for");          list.add("Geeks");          list.add("10");          list.add("20");           // Output the list          System.out.println("AbstractList: " + list);           // Remove the head using remove()          list.remove(3);           // Print the final list          System.out.println("Final AbstractList: " + list);           // getting the index of last occurrence          // using lastIndexOf() method          int lastindex = list.lastIndexOf("A");           // printing the Index          System.out.println("Last index of A : "                         + lastindex);      }  }  

Output
AbstractList: [Geeks, for, Geeks, 10, 20] Final AbstractList: [Geeks, for, Geeks, 20] Last index of A : -1


Methods in AbstractList

METHOD

DESCRIPTION

add​(int index, E element)Inserts the specified element at the specified position in this list (optional operation).
add​(E e)Appends the specified element to the end of 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).
clear()Removes all of the elements from this list (optional operation).
 equals​(Object o)Compares the specified object with this list for equality.
get​(int index)Returns the element at the specified position in this list.
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.
 iterator()Returns an iterator over the elements in this list in proper sequence.
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).
listIterator​(int index)Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
remove​(int index)Removes the element at the specified position in this list (optional operation).
 removeRange​(int fromIndex, int toIndex)Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive.
 set​(int index, E element)Replaces the element at the specified position in this list with the specified element (optional operation).
 subList​(int fromIndex, int toIndex)Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.


Methods declared in 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 declared in 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 declared in 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​(int index)Removes the element at the specified position in this list (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 AbstractList class in Java is an abstract class that provides a skeletal implementation of the List interface. It is part of the Java Collections Framework and is intended to be subclassed by concrete list implementations like ArrayList and LinkedList.

Here is an example of how you can use the AbstractList class in Java:

Java
import java.util.AbstractList; import java.util.List;  public class MyList extends AbstractList<Integer> {     private int size;      public MyList(int size) {         this.size = size;     }      @Override     public Integer get(int index) {         return index;     }      @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);         }     } } 

Output
0 1 2 3 4

By extending the AbstractList class, you only need to implement the get and size methods, which provides a basic implementation of a list. This can save you a lot of time and code compared to implementing the List interface from scratch.

Advantages of using AbstractList in Java:

  1. Reduced code duplication: By using the AbstractList class as a base, you can reduce the amount of code that you need to write to implement a list, since many of the common methods have already been implemented for you.
  2. Consistent behavior: Since the AbstractList class implements many of the methods in the List interface, you can be sure that your implementation will have consistent behavior with other list implementations, like ArrayList and LinkedList.

Disadvantages of using AbstractList in Java:

  1. Limited functionality: Since the AbstractList class is an abstract class, it provides only a basic implementation of a list. You may need to implement additional methods to provide the full functionality required by your application.
  2. Increased complexity: By extending the AbstractList class, you are increasing the complexity of your code, since you are relying on the base class to provide some of the behavior.

Reference Book: 

A good reference book for learning about the Java Collections Framework and AbstractList is "Java Collections" by Naftalin and Wadler. This book provides a comprehensive look at the Java collections framework, including AbstractList, and includes many examples and exercises to help you understand how to use these classes effectively.


Reference: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/AbstractList.html


Next Article
AbstractList add(E ele) method in Java with Examples

R

RishabhPrabhu
Improve
Article Tags :
  • Java
  • Java-Collections
  • Java - util package
  • Java-AbstractList
Practice Tags :
  • Java
  • Java-Collections

Similar Reads

    AbstractList in Java with Examples
    The AbstractList class in Java is a part of the Java Collection Framework and implements the Collection interface and the AbstractCollection class. AbstractList class provides a skeletal implementation of the List interface to minimize the effort required to implement this interface backed by a Rand
    7 min read
    AbstractList add(E ele) method in Java with Examples
    The add(E ele) method of AbstractList class in Java is used to insert the specified element to the end of the current list. Syntax: public boolean add(E ele) Where E is the type of element maintained by this AbstractList collection. Parameter: This method accepts a single parameter ele which represe
    2 min read
    AbstractList addAll() method in Java with Examples
    The addAll() method of java.util.AbstractList class is used to insert all of the elements in the specified collection into this list at the specified position. This shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elem
    5 min read
    AbstractList clear() method in Java with Examples
    The clear() method of java.util.AbstractList class is used to remove all of the elements from this list. The list will be empty after this call returns. Syntax: public void clear() Returns Value: This method does not return anything. Below are the examples to illustrate the clear() method. Example 1
    2 min read
    AbstractList equals() method in Java with Examples
    The equals() method of java.util.AbstractList class is used to compare the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e
    3 min read
    AbstractList get() method in Java with Examples
    The get() method of java.util.AbstractList class is used to return the element at the specified position in this list. Syntax: public abstract E get(int index) Parameters: This method takes index of the element as a parameter, the element at which is to be returned. Returns Value: This method return
    2 min read
    AbstractList hashCode() method in Java with Examples
    The hashCode() method of java.util.AbstractList class is used to return the hash code value for this list. Syntax: public int hashCode() Returns Value: This method returns the hash code value for this list. Below are the examples to illustrate the hashCode() method. Example 1: Java // Java program t
    2 min read
    AbstractList indexOf() method in Java with Examples
    The indexOf() method of java.util.AbstractList 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))), or -1 if t
    3 min read
    AbstractList iterator() method in Java with Examples
    The iterator() method of java.util.AbstractList class is used to return an iterator over the elements in this list in proper sequence. This implementation returns a straightforward implementation of the iterator interface, relying on the backing list's size(), get(int), and remove(int) methods. Synt
    2 min read
    AbstractList lastIndexOf() method in Java with Examples
    The lastIndexOf() method of java.util.AbstractList class is used to return the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the highest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences