In this chapter you will learn:
- What is List Interface in Java Collection?
- List Interface methods
- Iterator and ListIterator Interface
A list is an order collection which stores item at exact position where it is added. It doesn’t change items position so it is also known as sequence object. List allows duplicate entry so it may have duplicate element. There are 2 general purpose list implementations ArrayList
andLinkedList
. List interface provides following operations on elements.
- Positional Access: Each element can be accessed by their index position or numerical position so based on their numerical position it manipulates elements in the list. It includes method such as
get(), set(), add(), addall() and remove()
. - Search: Using
IndexOf
andlastIndexOf
method it searches for specified elements in the list and return their numerical position or index number. - Iteration: Using
listIterator()
method traverse lists in a sequential way. - Range-view: The
sublist()
method performs arbitrary range operations on the list.
Initialization
A list can be initialized as follow:
Listbooklist=new ArrayList ();
Methods
Methods | Descriptions |
void add(int index, object obj) |
Insert object at specified index position. The preexisting object shifted up |
boolean addAll(int index, Collection c) |
Inserts all elements of c into the invoking lists at the specified index position. The preexisting object shifted up. It returns true if any changes made into the list otherwise returns false. |
Object get(int index) |
Returns the object stored at the specified index position. |
int indexOf(Object obj) |
Returns index position of first instance of object in the list. If object not found it returns -1. |
int lastIndexOf(Object obj) |
Returns index position of last instance of object in the list. If object not found it returns -1. |
ListIterator listIterator() |
Returns an iterator to the start of the invoking list. |
ListIterator listIterator(int index) |
Returns an iterator to the invoking list that begins at the specified index. |
Object remove(int index) |
It removes the object from specified position and returns deleted objects. Subsequent objects index position shifted to left position or decremented by one. |
Object set(int index, Object obj) |
Assigns obj to the location specified by index within the invoking list. |
List subList(int start, int end) |
Returns a set of element ranging from specified start and end position in the invoking list. |
Programming Example
import java.util.*; class List_Example { public static void main(String[] args) { //Creating ArrayList List booklist=new ArrayList(); //Adding Item to list booklist.add("Java"); booklist.add("C#"); booklist.add("C++"); booklist.add("SQL"); booklist.add("Python"); //Printing List System.out.println(booklist); //Adding new item at index position 3 booklist.add(3, "PHP"); //Printing List System.out.println(booklist); //Getting index position of object SQL System.out.println(booklist.indexOf("SQL")); //Removing Object stored at index position 2 System.out.println(booklist.remove(2)); //Getting a sublist of object ranging from 2 to 4 System.out.println(booklist.subList(2,4)); } }
Output
[Java, C#, C++, PHP, SQL, Python]
4
C++
[PHP, SQL]
_
Iterator
iterator
is an object that implements either Iterator
or ListIterator
Interface. Iterator
Interface allows you to traverse through collections obtaining or removing elements. TheListIterator
Interface extends the Iterator and allows you to traverse collection in both direction forward and backward.
Methods and Description of ListIterator
Methods | Descriptions |
add(E e) |
It add element in the list at specified position. |
hasNext() |
This method returns true if iterator finds next element in the list while traversing forward direction. |
hasPrevious() |
It returns true if iterator finds next element in the list while traversing backward direction. |
next() |
It returns next element of the list and shift cursor to next position. |
nextIndex() |
Returns the index of the element that would be returned by a subsequent call to next(). |
previous() |
Returns the previous elements in the list and shift cursor position backwards. |
previousIndex() |
Returns the index of the element that would be returned by a subsequent call to previous(). |
remove() |
Removes element from the list. |
set(E e) |
Replaces the last element returned by next() or previous() with the specified element. |
Above is the list of methods that is used while traversing lists using iterators. You can understand its working style more clearly using following programming example.
Programming Example
import java.util.*; class List_Iterator { public static void main(String[] args) { //Creating ArrayList List booklist=new ArrayList(); //Adding Item to list booklist.add("Java"); booklist.add("C#"); booklist.add("C++"); booklist.add("SQL"); booklist.add("Python"); //Printing List System.out.println(booklist); //Using ListIterator //hasNext() Method System.out.print("Printing Forward Direction : "); ListIterator Ltr=booklist.listIterator(); while(Ltr.hasNext()) { Object element=Ltr.next(); System.out.print(element + " , "); } System.out.println(); //hasPrevious() Method System.out.print("Printing Backward Direction : "); while(Ltr.hasPrevious()) { Object element=Ltr.previous(); System.out.print(element + " , "); } System.out.println(); } }
Output
Printing Forward Direction : Java , C# , C++ , SQL , Python ,
Printing Backward Direction : Python , SQL , C++ , C# , Java ,
_
Summary
In this chapter you have learned how to use List Interface in java collection. You have also learned how to use Iterator
and ListIterator
Interface to traverse through list. Programming examples help you to understand topic more clearly. In the next chapter you will learn ArrayList class in Java.