ListIterator in Java with Examples

listiterator in java with examples

ListIterator:

List Iterator can be used to iterate List interfaces/List type of objects. [eg: ArrayList/LinkedList], But it can not be used to iterate Set/Map Interfaces.

ListIterator can be used to iterate in both forward and backward/reverse direction, where as in Iterator backward/reverse direction iteration is not possible.

 

Available methods in ListIterator:

 

  • add()
  • set()
  • remove()
  • previousIndex()
  • nextIndex()
  • next()
  • hasNext()
  • previous()
  • hasPrevious()

 

add():

Even after the listiterator created for the list, we can add the elements to the list using add() method. This can also be used inside the while loop.

Listiterator add() method example:

[java]

List<String> myList = new ArrayList<String>();
myList.add("Google");
ListIterator listIter = myList.listIterator();
listIter.add("Facebook");
System.out.println(myList);

[/java]

 

Output:

[plain]
[Facebook, Google]

[/plain]

Even, you can use add() method inside the while loop:

[java]

List<String> myList = new ArrayList<String>();
myList.add("Google");
ListIterator listIter = myList.listIterator();

while (listIter.hasNext()) {
listIter.add("Facebook");
listIter.add("Youtube");
System.out.println(myList);
System.out.println(listIter.next());
}

[/java]

 

Output:

[plain]

[Facebook, Youtube, Google]
Google

[/plain]

Remember: If you just comment the listIter.next() sysout in the whileloop, then loop will run in the infinite loop, because without doing anything we just added the values to the iterator.

set():

List iterator set() method will replace the list element which is currently returned by next() method in while loop. If you use set() method outside while then illegalstateexception will occur.

 

ListIterator set() method example:

[java]
List<String> myList = new ArrayList<String>();
myList.add("Google.com");
myList.add("Facebook.com");
ListIterator listIter = myList.listIterator();

while (listIter.hasNext()) {
if (listIter.next().toString().equalsIgnoreCase("Google.com")) {
listIter.set("google.co.in");
}
}

System.out.println(myList);

[/java]

 

Output:

[plain]
[google.co.in, Facebook.com]

[/plain]

Remember: If you use set() method outside the while loop you will be getting the IllegalStateException.

[java]
List<String> myList = new ArrayList<String>();
myList.add("Google.com");
myList.add("Facebook.com");
ListIterator listIter = myList.listIterator();

listIter.set("google.co.in");
System.out.println(myList);

[/java]

 

Exception: because set() should not be used outside the while loop. Because set() will replace the value returned by next()/previous() method. So should be used inside the loop only.

[plain]
Exception in thread "main" java.lang.IllegalStateException
at java.util.ArrayList$ListItr.set(Unknown Source)
at in.javadomain.IteratorListIterator.main(IteratorListIterator.java:17)

[/plain]

remove():

List iterator remove() method will remove the list element which is currently returned by next() method in while loop. If you use remove() method outside while then illegalstateexception will occur.

 

ListIterator remove() method example:

[java]
List<String> myList = new ArrayList<String>();
myList.add("Google.com");
myList.add("Facebook.com");
ListIterator listIter = myList.listIterator();
while (listIter.hasNext()) {
if (listIter.next().toString().equalsIgnoreCase("Google.com")) {
listIter.remove();
}
}

System.out.println(myList);

[/java]

 

Output:

[plain]

[Facebook.com]

[/plain]

 

Remember: If you use remove() method outside the while loop you will be getting the IllegalStateException. Because remove() and set() will handle the element which returned by next() method.

 

ListIterator remove() method used outside the while loop:

[java]
List<String> myList = new ArrayList<String>();
myList.add("Google.com");
myList.add("Facebook.com");
ListIterator listIter = myList.listIterator();

listIter.remove();
System.out.println(myList);

[/java]

 

Exception occured, because remove() method used outside the loop:

[plain]

Exception in thread "main" java.lang.IllegalStateException
at java.util.ArrayList$Itr.remove(Unknown Source)
at in.javadomain.IteratorListIterator.main(IteratorListIterator.java:17)

[/plain]

previousIndex()/nextIndex():

 

nextIndex() will return the index of the current element:

 

ListIterator nextIndex() method Example:

[java]

List<String> myList = new ArrayList<String>();
myList.add("Google.com");
myList.add("Facebook.com");
ListIterator listIter = myList.listIterator();
while (listIter.hasNext()) {
if (listIter.next().toString().equalsIgnoreCase("Google.com")) {
System.out.println(listIter.nextIndex());
}
}

[/java]

 

Output:

[plain]

1

[/plain]

previousIndex() will return the index of the previous element: If it is first element then it returns 0.

 

ListIterator previousIndex() method example:

[java]
List<String> myList = new ArrayList<String>();
myList.add("Google.com");
myList.add("Facebook.com");
ListIterator listIter = myList.listIterator();
while (listIter.hasNext()) {
if (listIter.next().toString().equalsIgnoreCase("Google.com")) {
System.out.println(listIter.previousIndex());
}
}

[/java]

 

Output:

[plain]

0

[/plain]

next()/hasNext():

hasNext() returns true, if it has more elements to traverse in the forward direction.
next() returns the next elements in the list and moves the cursor to the next/advanced position.

 

ListIterator next() method example:

[java]

List<String> myList = new ArrayList<String>();
myList.add("Google.com");
myList.add("Facebook.com");
ListIterator listIter = myList.listIterator();
while (listIter.hasNext()) {
System.out.println(listIter.next());
}

[/java]

 

Output:

[plain]
Google.com
Facebook.com

[/plain]

 

previous()/hasPrevious():

hasPrevious() returns true, if it has more elements to traverse in the backward/reverse direction.

previous() returns the previous elements in the list and moves the cursor position to backwards.

 

ListIterator previous() method Example:

[java]

List<String> myList = new LinkedList<String>();
myList.add("Google.com");
myList.add("Facebook.com");
myList.add("Couponzcorner.com");
ListIterator listIter = myList.listIterator();
while (listIter.hasNext()) {
String element = (String) listIter.next();
System.out.println("Forward Direction::: " + element);
listIter.set(element);
}

while (listIter.hasPrevious()) {
System.out.println("Reverse Direction:: " + listIter.previous());
}

[/java]

 

Output:

[plain]
Forward Direction::: Google.com
Forward Direction::: Facebook.com
Forward Direction::: Couponzcorner.com
Reverse Direction:: Couponzcorner.com
Reverse Direction:: Facebook.com
Reverse Direction:: Google.com

[/plain]

Leave a Reply