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:


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

 

Output:

[Facebook, Google]

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


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());
}

 

Output:


[Facebook, Youtube, Google]
Google

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:

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);

 

Output:

[google.co.in, Facebook.com]

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

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);

 

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.

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

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:

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);

 

Output:


[Facebook.com]

 

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:

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

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

 

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


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

previousIndex()/nextIndex():

 

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

 

ListIterator nextIndex() method Example:


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());
}
}

 

Output:


1

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

 

ListIterator previousIndex() method example:

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());
}
}

 

Output:


0

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:


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());
}

 

Output:

Google.com
Facebook.com

 

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:


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());
}

 

Output:

Forward Direction::: Google.com
Forward Direction::: Facebook.com
Forward Direction::: Couponzcorner.com
Reverse Direction:: Couponzcorner.com
Reverse Direction:: Facebook.com
Reverse Direction:: Google.com

563 total views, 4 views today

About: Mirthbees

Blogger, Innovator, Developer and Enterpreneur. Founder of GuruRecharge, Readymade online recharge script. Lead Developer in Bookrate.in