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. 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:
filter_none
edit
play_arrow
brightness_4
|
Output:
AbstractList:[Geeks, for, Geeks, 10, 20]
Methods in Java AbstractList:
Example:
filter_none
edit
play_arrow
brightness_4
|
Output:
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
- 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 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 the 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.
- list iterator(): Returns a list iterator over the elements in this list (in proper sequence).
- list iterator(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).
- remove range(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.
post a comment