The Iterator is the measure agency to traverse a collection inwards Java. You tin work Iterator to traverse a List, Set, Map, Stack, Queue or whatever Collection, but yous mightiness non know that in that place is about other agency to traverse over List inwards Java? Yes, it's called the ListIterator. There are many differences betwixt Iterator together with ListIterator inwards Java, but the most pregnant of them is that Iterator solely allows yous to traverse inwards one direction i.e. forward, yous accept merely got a next() method to acquire the adjacent element, in that place is no previous() method to acquire the previous element. On the other hand, ListIterator allows yous to traverse the listing inwards both directions i.e. forwards together with backward. It has got both next() together with previous() method to access the adjacent together with previous chemical constituent from List.
Unfortunately, ListIterator solely supports the List interface, yous cannot work it to traverse over Map or Set inwards Java. That was the basic difference betwixt Iterator together with ListIterator class, let's encounter a twain of to a greater extent than to reply this enquiry inwards item on the Java interviews.
Let's encounter each of them inwards piddling fleck detail.
Traversal direction
As I told yous inwards the get-go paragraph that key deviation betwixt Iterator together with ListIterator is that old allow traversal solely inwards forward direction i.e. yous cannot become dorsum 1 time yous motion forward, which is what sometimes yous need. ListIterator gives yous that functionality via previous() method. Similar to next() this method returns the previous chemical constituent thus back upward traversal inwards the backward management (see Core Java Volume 1 - Fundamentals yesteryear Cay. S. Horstmann)
Here is an instance of traversing a List inwards the contrary management :
Operation allowed during Iteration
Another key deviation betwixt ListIterator together with Iterator bird comes from the fact that yous tin perform lot of alteration functioning using ListIterator e.g. yous tin add together element, yous tin modify chemical constituent together with yous tin take chemical constituent using add(), set() together with remove() method, but yous tin solely take objects using Iterator interface every bit it solely got the remove() method. See chapter nineteen together with xx of Big Java: Early Objects yesteryear Cay S. Horstmann to larn to a greater extent than almost how to add together elements piece iterating over ListIterator inwards Java.
Supported Collection classes
Another affair which differentiates Iterator together with ListIterator is that Iterator is supported yesteryear most of the collection bird e.g. List, Set, Queue as good every bit Map which is non a Collection but constituent of Java's Collection framework. Since Collection bird implements the Iterable interface, which defines an iterator() method, all the concrete implementation of Collection classes e.g. ArrayList, LinkedList, Vector, HashSet, LinkedHashSet, TreeSet, PriorityQueue all supports traversal using Iterator.
On the other hand, ListIterator is obtained yesteryear calling the listIterator() method which is solely defined inwards the List interface, thus it's solely available to concrete implementation of List interface e.g. ArrayList, LinkedList together with Vector class. You tin read Core Java for Impatient to larn to a greater extent than almost these classes together with Java Collection framework.
Iteration from whatever index
One of the unique features of List interface is that it provides about other overloaded listIterator(int index) method which tin live on used to strat traversal from whatever arbitrary index. Unfortunately, Iterator doesn't back upward this functionality because it has to operate over collection which doesn't back upward index-based access e.g. Set together with Queue interfaces. Anyway, You tin easily write an instance of iterating over List starting at a special index inwards Java using ListIterator.
Supported Methods
Last deviation betwixt Iterator together with ListIterator inwards this listing is structural. ListIterator seems to acquire to a greater extent than characteristic than Iterator thus got to a greater extent than methods e.g.
hasPrevious() to banking concern lucifer whether the listing has got to a greater extent than elements piece traversing the listing inwards backward direction
next() which returns the adjacent chemical constituent inwards the listing together with moves the cursor forward.
previous() which returns the previous chemical constituent inwards the listing together with moves the cursor backward.
nextIndex() which returns the index of the adjacent chemical constituent inwards the list.
previousIndex() which returns the index of the previous chemical constituent inwards the list.
remove() to take the electrical flow chemical constituent inwards the collection i.e chemical constituent returned yesteryear next() or previous(). See how to take object using Iterator for to a greater extent than details.
set(E e) to supplant the electrical flow chemical constituent i.e chemical constituent returned by next() or previous() alongside the specified element, and
add(E e) to insert the specified chemical constituent inwards the list
In short, hither is a squeamish summary of differences betwixt ListIterator together with Iterator interface inwards Java:
That's all almost the difference betwixt Iterator together with ListIterator inwards Java. The primary payoff of Iterator is that your code volition piece of work fifty-fifty if yous transcend a List or Set because yous volition live on working alongside a Collection, but when yous work the ListIterator, together with so your code volition solely piece of work alongside the List.
Further Learning
Java Fundamentals: Collections
From Collections to Streams inwards Java 8 Using Lambda Expressions
What's New inwards Java 8
Unfortunately, ListIterator solely supports the List interface, yous cannot work it to traverse over Map or Set inwards Java. That was the basic difference betwixt Iterator together with ListIterator class, let's encounter a twain of to a greater extent than to reply this enquiry inwards item on the Java interviews.
Iterator vs ListIterator
You tin differentiate betwixt Iterator together with ListIterator on next topics :- Direction of traversal
- Operation allowed during iteration
- Supported Collection classes
- Iterating from whatever arbitrary element
- Supported methods
Let's encounter each of them inwards piddling fleck detail.
Traversal direction
As I told yous inwards the get-go paragraph that key deviation betwixt Iterator together with ListIterator is that old allow traversal solely inwards forward direction i.e. yous cannot become dorsum 1 time yous motion forward, which is what sometimes yous need. ListIterator gives yous that functionality via previous() method. Similar to next() this method returns the previous chemical constituent thus back upward traversal inwards the backward management (see Core Java Volume 1 - Fundamentals yesteryear Cay. S. Horstmann)
Here is an instance of traversing a List inwards the contrary management :
import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.ListIterator; /** * Java Program to demonstrate deviation betwixt Iterator together with ListIterator inwards * Java. * * @author WINDOWS 8 * */ public class IteratorAndListIterator{ public static void main(String[] args) { List<String> wishlist = new ArrayList<>(); wishlist.add("Bose Bluetooth HeadSet"); wishlist.add("Kindle Fire HD10"); wishlist.add("Good HDMI Cable"); wishlist.add("128GB Micro SD Card"); wishlist.add("Good Blueray Player"); // Iterating inwards forwards direction Iterator<String> itr = wishlist.iterator(); StringBuilder sb = new StringBuilder(); while (itr.hasNext()) { sb.append(itr.next()).append(","); } System.out.println("forward lodge list: " + sb.toString()); // Iterating inwards contrary direction // getting ListIterator from final Index ListIterator<String> listItr = wishlist.listIterator(wishlist.size()); sb = new StringBuilder(); while (listItr.hasPrevious()) { sb.append(listItr.previous()).append(" "); } System.out.println("reverse lodge list: " + sb.toString()); } } Output forwards lodge list: Bose Bluetooth HeadSet,Kindle Fire HD10, Good HDMI Cable,128GB Micro SD Card,Good Blueray Player, contrary lodge list: Good Blueray Player 128GB Micro SD Card Good HDMI Cable Kindle Fire HD10 Bose Bluetooth HeadSet
Operation allowed during Iteration
Another key deviation betwixt ListIterator together with Iterator bird comes from the fact that yous tin perform lot of alteration functioning using ListIterator e.g. yous tin add together element, yous tin modify chemical constituent together with yous tin take chemical constituent using add(), set() together with remove() method, but yous tin solely take objects using Iterator interface every bit it solely got the remove() method. See chapter nineteen together with xx of Big Java: Early Objects yesteryear Cay S. Horstmann to larn to a greater extent than almost how to add together elements piece iterating over ListIterator inwards Java.
Supported Collection classes
Another affair which differentiates Iterator together with ListIterator is that Iterator is supported yesteryear most of the collection bird e.g. List, Set, Queue as good every bit Map which is non a Collection but constituent of Java's Collection framework. Since Collection bird implements the Iterable interface, which defines an iterator() method, all the concrete implementation of Collection classes e.g. ArrayList, LinkedList, Vector, HashSet, LinkedHashSet, TreeSet, PriorityQueue all supports traversal using Iterator.
On the other hand, ListIterator is obtained yesteryear calling the listIterator() method which is solely defined inwards the List interface, thus it's solely available to concrete implementation of List interface e.g. ArrayList, LinkedList together with Vector class. You tin read Core Java for Impatient to larn to a greater extent than almost these classes together with Java Collection framework.
Iteration from whatever index
One of the unique features of List interface is that it provides about other overloaded listIterator(int index) method which tin live on used to strat traversal from whatever arbitrary index. Unfortunately, Iterator doesn't back upward this functionality because it has to operate over collection which doesn't back upward index-based access e.g. Set together with Queue interfaces. Anyway, You tin easily write an instance of iterating over List starting at a special index inwards Java using ListIterator.
Supported Methods
Last deviation betwixt Iterator together with ListIterator inwards this listing is structural. ListIterator seems to acquire to a greater extent than characteristic than Iterator thus got to a greater extent than methods e.g.
hasPrevious() to banking concern lucifer whether the listing has got to a greater extent than elements piece traversing the listing inwards backward direction
next() which returns the adjacent chemical constituent inwards the listing together with moves the cursor forward.
previous() which returns the previous chemical constituent inwards the listing together with moves the cursor backward.
nextIndex() which returns the index of the adjacent chemical constituent inwards the list.
previousIndex() which returns the index of the previous chemical constituent inwards the list.
remove() to take the electrical flow chemical constituent inwards the collection i.e chemical constituent returned yesteryear next() or previous(). See how to take object using Iterator for to a greater extent than details.
set(E e) to supplant the electrical flow chemical constituent i.e chemical constituent returned by next() or previous() alongside the specified element, and
add(E e) to insert the specified chemical constituent inwards the list
In short, hither is a squeamish summary of differences betwixt ListIterator together with Iterator interface inwards Java:
That's all almost the difference betwixt Iterator together with ListIterator inwards Java. The primary payoff of Iterator is that your code volition piece of work fifty-fifty if yous transcend a List or Set because yous volition live on working alongside a Collection, but when yous work the ListIterator, together with so your code volition solely piece of work alongside the List.
Other Java Collection articles yous may similar to explore
- Difference betwixt fail-safe together with fail-fast Iterator inwards Java? (answer)
- Difference between Hashtable together with HashMap inwards Java? (answer)
- Difference betwixt TreeMap together with TreeSet inwards Java? (answer)
- Difference between ArrayList together with LinkedList inwards Java? (answer)
- Difference betwixt HashMap together with LinkedHashMap inwards Java? (answer)
- Difference between EnumMap together with HashMap inwards Java
- Difference between ArrayList together with HashSet inwards Java? (answer)
- Difference betwixt HashSet together with TreeSet inwards Java? (answer)
- Difference between HashMap together with ConcurrentHashMap inwards Java? (answer)
- Difference betwixt Vector together with ArrayList inwards Java? (answer)
- Difference betwixt IdentityHashMap, WeakHashMap, together with EnumMap inwards Java? (answer)
Further Learning
Java Fundamentals: Collections
From Collections to Streams inwards Java 8 Using Lambda Expressions
What's New inwards Java 8


0 Response to "5 Difference betwixt Iterator as well as ListIterator inward Java?"
Posting Komentar