List Interface implemented classes

List Interface implemented classes


List Interface
Java.util.List is a child interface of Collection.

As you know the List is an ordered collection of objects.In the List duplicate values can be stored. So we can iterate the list index basis Since List preserves the insertion order it allows positional access and insertion of elements. List Interface is implemented, four classes

1-ArrayList

2-LinkedList

3-Vector

4-Stack classes.

Creating List Objects: 
The list is an interface, we can create an instance of List in following ways:

List a = new ArrayList();
List b = new LinkedList();
List c = new Vector();
List d = new Stack();


Generic List Object:


Generics features introduced in Java 1.5. In the generics, it is possible to bind the type of object that can be stored in the List. We can declare type safe List as like.

// Obj is the type of object to be stored in List.
List<Obj> list = new List<Obj> ();


Operations on List:
By The List, Interface extends Collection, so it supports all the operations of Collection Interface and along with following operations:

Positional Access(index basis access):
A list allows multiple operations like add, remove, get and set operations based on numerical positions(index basis) of elements in List. The list provides multiple methods for these operations:

void add(int index, Object O): 
This method adds given element at specified index. In the list, you can add any element in given index position.


boolean addAll(int index, Collection c): 
This method has two arguments. First is index and another is Collection obj.This method adds all elements from the specified collection to list. The first element gets inserted at 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.

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.

Object get(int index): 
This method returns the element at the specified index.

Object set(int index, Object new): 

This method replaces element at given index with the new element. This function returns the element which was just replaced by new element.

Related Articles

post a comment