The AbstractList class in Java is a part of the Java Collection Framework and implements the Collection interface and the AbstractCollection class. It is used to implement an unmodifiable list, for which one needs to only extend this AbstractList Class and implement only the get() and the size() methods.
Class Hierarchy:
java.lang.Object
↳ java.util.AbstractCollection<E>
↳ Class AbstractList<E>
Syntax:
public abstract class AbstractList<E>
extends AbstractCollection<E>
implements List<E>
where E is the type of elements maintained
by this collection.
Constructors in Java AbstractList:
- protected AbstractList(): The default constructor, but being protected, it doesn’t allow to create an AbstractList object.
Below is the sample program to illustrate AbstractList in 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 LinkedList<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); } } |
AbstractList:[Geeks, for, Geeks, 10, 20]
Methods in Java AbstractList:
- add(E e): Appends the specified element to the end of this list (optional operation).
- add(int index, E element): Inserts the specified element at the specified position in 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.
- AbstractList get() method in Java with Examples
- AbstractList add(E ele) method in Java with Examples
- AbstractList set() Method in Java with Examples
- AbstractList equals() method in Java with Examples
- AbstractList remove() Method in Java with Examples
- AbstractList indexOf() method in Java with Examples
- AbstractList lastIndexOf() method in Java with Examples
- AbstractList listIterator() Method in Java with Examples
- AbstractList iterator() method in Java with Examples
- AbstractList addAll() method in Java with Examples
- AbstractList subList() method in Java with Examples
- AbstractList clear() method in Java with Examples
- AbstractList hashCode() method in Java with Examples
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java lang.Long.highestOneBit() method in Java with Examples
Example:
// 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 occurence // using lastIndexOf() method int lastindex = list.lastIndexOf("A"); // printing the Index System.out.println("Last index of A : " + lastindex); } } |
AbstractList: [Geeks, for, Geeks, 10, 20] Final AbstractList: [Geeks, for, Geeks, 20] Last index of A : -1
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/AbstractList.html
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



