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.
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.
l.add(x);
l.add(y);
if you don’t want element to be inserted one after another then
- add(int index, Object o) :- Add at particular index.
- addAll(int index, Object o) :- Add objects starting from given index.
- l.indexOf(“A”) :- If you want to check position of particular object.
- l.lastIndexOf(“A”) :- If you want to check last occurrence.
- get(int index) :- If you want to retrieve object placed at particular index.
- listIterator i = ListIterator() :- Retrieve objects one by one
- Set(int index, Object o) :- Replace an object at particular index.
List interface specific methods
- void add(int index, Object o)
- boolean addAll(int index, Collection c)
- Object get(int index)
- Object set(int index, Object o)
- int indexOf((Object o)
- int lastIndexOf((Object o)
- 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
- ArrayList
- Vector
- LinkedList
- Stack