The list is an interface that provides Ordered Collection, the Ordered collection is the same order in which they are inserted. The List interface is a child of the collection interface. It can be used to insert, delete, and access the elements from the list. The list interface is implemented by the ArrayList, LinkedList, Vector, and Stack classes. It can be used to insert, delete, and access the elements from the list and it can contain duplicate values.
Methods to create list objects
List a = new ArrayList();
List b = new LinkedList ();
List c = new Vector();
List d = new Stack();
The type-safe List can be defined as follows
List list = new ArrayList ();
Positional Access
The list allows add, remove, get, and set operations based on the numerical positions of elements. The list provides the following methods
add
void add(int index, Object O): This method adds a given element at the specified index.
boolean addAll(int index, Collection c): This method adds all elements from a specified collection to the list. The first element gets inserted at the given index. If there is already an element at that position, that element, and other subsequent elements (if any) are shifted to the right by increasing their index.
remove
Object remove (int index): This method removes an element from the specified index. It shifts subsequent elements (if any) to left and decreases their indexes by 1.
Get method
Object get (int index): This method returns an element at the specified index.
Set method
Object set (int index, Object new): This method replaces an element at a given index with a new element. This function returns the element which was just replaced by a new element.
Search
The list provides methods to search elements and returns their numeric position. The following t methods are supported by List for this operation
index
int indexOf(Object o): This method returns the first occurrence of a given element or -1 if the element is not present in the list.
int lastIndexOf(Object o): This method returns the last occurrence of a given element or -1 if the element is not present in the list.
Iteration
List Iterator (extends Iterator) is used to iterate over List element. The list iterator is a bidirectional iterator.
Range-view
List subList(int fromIndex, int toIndex)This method returns the List view of a specified List between from Index(inclusive) and to Index(exclusive).
-> ArrayList internally uses a dynamic array to store the elements.
-> The ArrayList class implements the List interface and it acts as a list.
-> It uses a dynamic array to store the duplicate elements of different data types.
-> The ArrayList class maintains the insertion order and is non-synchronized.
-> The elements stored in the ArrayList class can be randomly accessed.
Use case demonstrating ArrayList
Output:
-> LinkedList implements the Collection interface.
-> It uses a doubly linked list internally to store the elements.
-> It can store the duplicate elements. It maintains the insertion order and is not synchronized.
-> In LinkedList, the manipulation is fast because no shifting is required
Use case demonstrating linkedlist
Output:
-> Vector uses a dynamic array to store the data elements.
-> It is similar to ArrayList. However, It is synchronized and contains many methods that are not the part of Collection framework
Use case demonstrating vector
Output:
->The stack is the subclass of Vector.
-> It implements the last-in-first-out data structure, i.e., Stack. The stack contains all of the methods of the Vector class
-> It provides methods like boolean push(), boolean peek(), boolean push(object o), which defines its properties.
Use case demonstrating stack