List Concept Simplified In Java

List Concept Simplified In Java
List Concept Simplified In Java
What is a List:

The list concept is the child interface of collection. Collection interface was introduced in java 1.2 version and  List Interface got introduced in the same version. The List interface contains an ordered sequence of elements available in a collection. It allows duplicate elements in the list. The List interface inherits the methods of Collection interface.

Inside the list interface, there are 3 primary classes. They are…

Ads code goes here
  1. ArrayList-1.2 version
  2. AbstractSequentialList-1.2 Version
  3. Vector-1.0 version (Legacy class)

2.1 LinkedList-1.2 Version
3.1 Stack (1.0 version-legacy class)

When we should use a list:

If we want to represent a group of individual objects as a single entity where duplicates are allowed and insertion order preserved then we should go for List interface.
In Java 1.2 version Vector, Stack classes are re-engineered to implement List interface.

As we can preserve the insertion order hence index will be generated. We can differentiate duplicate objects by using Index. So Index plays an important role in List.

List concept Interface is having the following methods:

void add(int index,Object o);//adds the element 'o' in the specified index.
void add(int index,Collection c);//adds the elements of the collection c at the specified index.
boolean addAll(int index,Collection c);//adds the elements of the collection c at the specified index.
Object get(int index);//returns the element available in the specified index.
Object remove(int index);//removes the element available in the specified index.
Object set(int index,Object new);//This method is very interesting, it replaces the element present at
// specified index with provided object and returns old object]
int indexOf(Object o); //returns the index of first occurrence of o,if it does not available it returns -1
int lastIndexOf(Object o);//returns the index of last occurrence of o,if it does not available it returns -1
ListIterator listIterator();//returns a listIterator of the elements
ListIterator listIterator(int index);//returns a listIterator of the elements from the specified index.
void set(int index,Object o);//replaces the element in the specified index with specified element.
List subList(int stratIndex,int endIndex);//returns the elements available from the specified boundaries.

READ  Runnable interface in Java

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *