List is the sequence of elements, or we can say collection of elements, on which user has precise control over where an elements are inserted. These elements can be accessed by their index value (Starting from 0) which is of Integer type and with the help of this index, user can search any element easily.
Important Note: List allows duplicate values as well as null values in it.
List is a child of Collection Interface, In addition to adding all methods implemented in Collection Interface, List adds its own methods to add or delete elements on the bases of index that are discussed below.
Some of these methods can throw exceptions, (like UnsupportedOperationException) if the given collection cannot be modified and Exception ClassCastException is generated if given object is not compatible with other.
Table of Contents
List Interface extends Collection Interface which further extends Iterable Interface, as shown in figure 1.
ArrayList
ArrayList is a Class that is implementation of List Interface, that allows elements to be added and removed dynamically from it. If in case more elements are added than its capacity, Arraylist automatically increases its size. Elements in it can be accessed directly using get() and set() methods using index value, but add() and remove() operations has very low performance as all the elements are shifted by this operation leading to change in index values.
LinkedList
LinkedList is a class that is also the implementation of List Interface which is implemented using Doubly Linked List, It allows elements to be added dynamically in it, if in case more elements are added than its capacity, linkedlist automatically increases its size. Operations such as add() and remove() performs better than arrayList whereas operations such as get() and set() performs very poor as compared to arrayList.
Vectors
Vector is also the implementation of List Interface. Points that distinguishes vector from arraylist and linkedlist are vectors are Synchronized and also vector is a legacy class that is, it is introduced before JDK 1.2. and it do not implement most of the collection methods.
Let us discuss List Interface with the help of program. In this program objects of all the List implementations (Vector, ArrayList, LinkedList) are created and data of String type is added and then displayed, we have divided this program into 3 Steps that we will discuss one by one.
import java.util.*; public class ListInterfaceDemo{ public static void main(String args[]){ //Step 1: Create Objects List<String> arrayList = new ArrayList<String>(); List<String> linkedList= new LinkedList<String>(); List<String> vectorObject= new Vector<String>(); //Step 2: Add values arrayList.add("This"); arrayList.add("is"); arrayList.add("ArrayList"); linkedList.add("This"); linkedList.add("is"); linkedList.add("LinkedList"); vectorObject.add("This"); vectorObject.add("is"); vectorObject.add("vector"); //Step 3:Display values System.out.println("ArrayList: "+arrayList); System.out.println("LinkedList: "+linkedList); System.out.println("Vector : "+vectorObject); } }
Output:
ArrayList: [This, is, ArrayList] LinkedList: [This, is, LinkedList] Vector : [This, is, vector]
Description:
1. void add(int index, Object obj)
This method adds specified element at the specified index, moving the entire elements one step up, avoiding any type of overlapping of element.
2. boolean addAll(int index, Collection c)
This method adds all elements of Collection c after the specified index, moving the entire elements up, avoiding any type of overlapping, This method returns true if the operation is successful or else false.
3. Object get(int index)
This method retrieves element at the specified index.
4. int indexOf(Object obj)
This method retrieves the first occurrence of the given element in the List or it will return -1 if there is no such elements.
5. int lastIndexOf(Object obj)
This method retrieves the last occurrence of the given element in the List or it will return -1 if there is no such elements.
6. ListIterator listIterator( )
This method gives object of ListIterator from the index 0 of the list, to traverse all the elements from the start.
7. ListIterator listIterator(int index)
This method gives object of ListIterator starting from the specified index of the list to traverse all the elements from the start of that index.
8. Object remove(int index)
This method removes an element from the list at specified index, In addition to removing this method also returns removed element. After deleting that element indexes of subsequent elements in the list are decreased by 1.
9. Object set(int index, Object obj)
This method sets the element at the specified index, if there is already any element present than that element is replaced by the new one.
10. List subList(int start, int end)
This method returns the sub-list from the index “start” to index “end”.
Premium Project Source Code:
Leave a Reply