Abstract List in Java
In Java, the Abstract List is the part of the Java Collection Framework. The Abstract list is implemented by the collection interface and the Abstract Collection class. This is used when the list can not be modified. To implement this AbstractList class is used with get() and size() methods.
Below is the class Hierarchy
Syntax:
public abstract class AbstractList<E> extends AbstractCollection<E>implements List<E>
Below are the methods of AbstractList Class
S.no. |
Method |
Description |
1 |
add(E e) |
It is used to add an element to the end of the list. |
2 |
add(int index, E element) |
It is used to add an element at the specified position of the list. |
3 |
add(int index, Collection c) |
It is used to add an element in the specified collection in the specified position. |
4 |
clear() |
It is used to remove all the elements from the list. |
5 |
equals(Object o) |
It is used to compare an element from another element in the list. |
6 |
get(int index) |
It is used to get the element from the specified position in the list. |
7 |
hashCode() |
It is used to get hash code from the list. |
8 |
indexOf(Object o) |
It is used to get the first element from the list. If the list is empty then it returns -1. |
9 |
Iterator() |
It returns all the elements from the list. Using Iterator. |
10 |
lastIndexOf(Object o) |
It is used to get the Last element from the list. If the list is empty then it returns -1. |
11 |
listIterator() |
It is used to get the Iterated list in a proper sequence. |
12 |
remove(int index) |
It is used to remove the specified element from the list. |
13 |
removeRange(intfromIndex, inttoIndex) |
It is used to remove an element from the specified range. |
14 |
set(int index, E element) |
It is used to replace an element from the specified element. |
15 |
subList(intfromIndex, inttoIndex) |
It is used to get elements from the specified range. |
Example:
import java.util.*;
public class AbstractListDemo1 {
public static void main(String args[])
{
AbstractList list = new LinkedList();
list.add("Dog");
list.add("Cat");
list.add("Bird");
list.add("Tiger");
list.add("Rabit");
System.out.println("***********************************");
System.out.println("Elements in the List 1:" + list);
list.add(3, "Deer");
System.out.println("***********************************************************");
System.out.println("After Adding Deer at position 3");
System.out.println("Elements in the List 1:" + list); //New List
System.out.println("***********************************************************");
AbstractList list1 = new LinkedList();
list1.add("Dog");
list1.add("Cat");
list1.add("Bird");
list1.add("Tiger");
list1.add("Rabit");
System.out.println("***********************************************************");
System.out.println("Elements in the List 2 :" + list1); //New List2
System.out.println("***********************************************************");
boolean ab = list.equals(list1);
System.out.println("Are both list equal : "+ ab);
System.out.println("***********************************************************");
String bc = list1.get(3);
System.out.println("Get element of index 3 : "+bc);
System.out.println("***********************************************************");
int cd= list.hashCode();
System.out.println("HashCode : " + cd);
System.out.println("***********************************************************");
int de= list.indexOf("Bird");
System.out.println("index of Bird in List 1: " + de);
System.out.println("***********************************************************");
Iterator ef = list.iterator();
while (ef.hasNext())
{
System.out.println("Element : "+ ef.next());
}
System.out.println("***********************************************************");
intfg= list.lastIndexOf("Rabit");
System.out.println("Last index of Rabit : "+ fg);
System.out.println("***********************************************************");
ListIteratorgh = list.listIterator(3);
System.out.println("The list is as follows:");
while (gh.hasNext()) {
System.out.println(gh.next());
}
System.out.println("***********************************************************");
}
}
Immutable List
In Java, Immutable List is immutable. The Elements of the Immutable list are Fixed. It means the elements of the list are read-only. If we try to add, delete or update any element then it will throw UnsupportedOperationException. It also does not allow null elements. If we try to create a null element then it will throw NullPointerException and if we try to add null element then it will throw UnsupportedOperationException.
Following are the Advantages of the ImmutableList.
1. It is threaded safe.
2. The memory is well organized.
Below is the class Hierarchy