In Java there is an Iterator interface which provides an iterator over a collection (List, Set etc.) but there is another interface ListIterator in Java which provides an iterator exclusively for lists like ArrayList, LinkedList, CopyOnWriteArrayList.
While iterator can only move forward, ListIterator provides functionality to traverse the List in either direction; forward or backward, which is one of the difference between Iterator and ListIterator in Java. Other differences are as follows.
- Both of these interfaces have remove() method to safely remove an element from List after the iterator is created but ListIterator has an
add()
method too. - ListIterator also has a
set()
method to change the element while iterating a List.
No current element in ListIterator
A ListIterator has no current element; its cursor position always lies between the element that would be returned by a
call to previous()
and the element that would be returned by a call to next()
. Following image shows the possible cursor positions for a list of length n.
Java ListIterator methods
ListIterator interface in Java provides the following methods-
- add(E e)- Inserts the specified element into the list.
- hasNext()- Returns true if this list iterator has more elements when traversing the list in the forward direction.
- hasPrevious()- Returns true if this list iterator has more elements when traversing the list in the reverse direction.
- next()- Returns the next element in the list and advances the cursor position.
- nextIndex()- Returns the index of the element that would be returned by a subsequent call to next().
- previous()- Returns the previous element in the list and moves the cursor position backwards.
- previousIndex()- Returns the index of the element that would be returned by a subsequent call to previous().
- remove()- Removes from the list the last element that was returned by next() or previous().
- set(E e)- Replaces the last element returned by next() or previous() with the specified element.
Java ListIterator example with bi-directional traversal
import java.util.LinkedList; import java.util.List; import java.util.ListIterator; public class ListIterationDemo { public static void main(String[] args) { List<String> carList = new LinkedList<String>(); carList.add("Audi"); carList.add("Jaguar"); carList.add("BMW"); carList.add("Mini Cooper"); //Getting ListIterator ListIterator<String> ltr = carList.listIterator(); //forward iteration System.out.println("List iteration - forward direction"); while(ltr.hasNext()){ System.out.println(ltr.next()); } // backward iteration System.out.println("List iteration - backward direction"); while(ltr.hasPrevious()){ System.out.println(ltr.previous()); } } }Output
List iteration - forward direction Audi Jaguar BMW Mini Cooper List iteration - backward direction Mini Cooper BMW Jaguar Audi
Example using add() and set() method of ListIterator
import java.util.LinkedList; import java.util.List; import java.util.ListIterator; public class ListIterationDemo { public static void main(String[] args) { List<String> carList = new LinkedList<String>(); carList.add("Audi"); carList.add("Jaguar"); carList.add("BMW"); carList.add("Mini Cooper"); //Getting ListIterator ListIterator<String> ltr = carList.listIterator(); while(ltr.hasNext()){ String car = ltr.next(); if(car.equals("BMW")) { ltr.add("Mercedes"); } if(car.equals("Mini Cooper")) { ltr.set("Camry"); } } System.out.println("List elements- " + carList); } }output
List elements- [Audi, Jaguar, BMW, Mercedes, Camry]
That's all for the topic Java ListIterator With Examples. If something is missing or you have something to share about the topic please write a comment.
You may also like
- Fail-fast And Fail-safe Iterators in Java
- Java Immutable List With Examples
- ArrayList Internal Implementation in Java
- Java Volatile Keyword With Examples
- throw Vs throws in Java Exception Handling
- How to Read Delimited File in Java
- Java Date Difference Program
- java.time.Duration Class With Examples
- Spring @Required Annotation
- Mapper Only Job in Hadoop MapReduce
No comments:
Post a Comment