This class provides a skeletal implementation of the List interface in order to reduce the effort required to implement this interface in combination with a “random access” data store (such as an array). Abstract List should be used instead of this class for sequential access data (such as a linked list).
Declaration:
public abstract class AbstractList<E> extends AbstractCollection<E>
implements List<E>
Methods of Abstract List Class:
Method | Description |
---|---|
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. |
Example:
import java.util.LinkedList;
import java.util.AbstractList;
public class Demo {
public static void main(String[] args) {
AbstractList<Integer> myList = new LinkedList<Integer>();
myList.add(50);
myList.add(100);
myList.add(150);
myList.add(200);
myList.add(250);
myList.add(300);
myList.add(350);
myList.add(400);
System.out.println("Elements in the AbstractList class = " + myList);
myList.remove(3);
// Print the final list
System.out.println("Final AbstractList: " + myList);
// getting the index of last occurrence
// using lastIndexOf() method
int lastindex = myList.lastIndexOf("A");
// printing the Index
System.out.println("Last index of A : "
+ lastindex);
}
}
Output:
Elements in the AbstractList class = [50, 100, 150, 200, 250, 300, 350, 400]
Final AbstractList: [50, 100, 150, 250, 300, 350, 400]
Last index of A : -1
ImmutableList in Java:
- ImmutableList, as the name implies, is an immutable type of List. It means that the List’s contents are fixed or constant after declaration, i.e. they are read-only.
- UnsupportedOperationException is thrown whenever an attempt is made to add, delete, or update elements in the List.
- An ImmutableList does not support null elements.
- NullPointerException is thrown if an attempt is made to create an ImmutableList with a null element. UnsupportedOperationException is thrown if an attempt is made to add a null element to a List.
ImmutableList Benefits:
- Thread safety is ensured.
- They are memory efficient.
- As they are immutable, they can be passed to third-party libraries without issue.
Note: also read about the File class in Java
Follow Me
If you like my post, please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Leave a Comment