Skip to content

Java List Interface

  • by

List(I) Interface:

Contains all the methods of collection and has its own method.

  • The list is the child interface of the collection.
  • 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.
java list collection

Legacy classes (vector and stack):-Classes coming from older version/generation are called as legacy classes.

  • We can differentiate duplicate by using index.
  • We can preserve insertion order by using index, hence index plays very important role in
    list interface.
list interface

l.add(x);
l.add(y);

if you don’t want element to be inserted one after another then

  1.  add(int index, Object o) :- Add at particular index.
  2.  addAll(int index, Object o) :- Add objects starting from given index.
  3.  l.indexOf(“A”) :- If you want to check position of particular object.
  4.  l.lastIndexOf(“A”) :- If you want to check last occurrence.
  5.  get(int index) :- If you want to retrieve object placed at particular index.
  6.  listIterator i = ListIterator() :- Retrieve objects one by one
  7.  Set(int index, Object o) :- Replace an object at particular index.

List interface specific methods

  1. void add(int index, Object o)
  2. boolean addAll(int index, Collection c)
  3. Object get(int index)
  4. Object set(int index, Object o)
  5. int indexOf((Object o)
  6. int lastIndexOf((Object o)
  7. ListIterator listIterator()

Example :

import java.awt.List;
import java.util.ArrayList;
import java.util.ListIterator;

public class CollectionList {
	
	public static void main(String arg[]){
		ArrayList<String> list=new ArrayList<String>();
		list.add("Ajay");
		list.add("Rahul");
		list.add("Zos");
		
		System.out.println("Array List example");
		System.out.println(list.toString());
	}
}

Output :

Array List example
[Ajay, Rahul, Zos]

Implementation classes

  1. ArrayList
  2. Vector
  3. LinkedList
  4. Stack

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading