Saturday, November 23, 2019

5 Divergence Betwixt Iterator Too Listiterator Inward Java?

The Iterator is the criterion agency to traverse a collection inward Java. You tin sack utilisation Iterator to traverse a List, Set, Map, Stack, Queue or whatever Collection, but y'all mightiness non know that at that spot is closed to other agency to traverse over List inward Java? Yes, it's called the ListIterator. There are many differences betwixt Iterator in addition to ListIterator inward Java, but the most pregnant of them is that Iterator solely allows y'all to traverse inward one direction i.e. forward, y'all accept merely got a next() method to acquire the side past times side element, at that spot is no previous() method to acquire the previous element. On the other hand, ListIterator allows y'all to traverse the listing inward both directions i.e. frontwards in addition to backward. It has got both next() in addition to previous() method to access the side past times side in addition to previous chemical constituent from List.

Unfortunately, ListIterator solely supports the List interface, y'all cannot utilisation it to traverse over Map or Set inward Java. That was the basic difference betwixt Iterator in addition to ListIterator class, let's come across a duo of to a greater extent than to reply this inquiry inward special on the Java interviews.


Iterator vs ListIterator

You tin sack differentiate betwixt Iterator in addition to ListIterator on next topics :
  • Direction of traversal
  • Operation allowed during iteration
  • Supported Collection classes
  • Iterating from whatever arbitrary element
  • Supported methods

Let's come across each of them inward petty flake detail.




Traversal direction
As I told y'all inward the starting fourth dimension paragraph that primal departure betwixt Iterator in addition to ListIterator is that erstwhile allow traversal solely inward forward direction i.e. y'all cannot conk dorsum 1 time y'all motion forward, which is what sometimes y'all need. ListIterator gives y'all that functionality via previous() method. Similar to next() this method returns the previous chemical constituent thence back upwardly traversal inward the backward management  (see Core Java Volume 1 - Fundamentals past times Cay. S. Horstmann)

Here is an event of traversing a List inward the opposite management :

import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.ListIterator;  /**  * Java Program to demonstrate departure betwixt Iterator in addition to ListIterator inward  * 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 inward frontwards 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 inward opposite 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 frontwards lodge list: Bose Bluetooth HeadSet,Kindle Fire HD10, Good HDMI Cable,128GB Micro SD Card,Good Blueray Player, opposite lodge list: Good Blueray Player 128GB Micro SD Card  Good HDMI Cable Kindle Fire HD10 Bose Bluetooth HeadSet


Operation allowed during Iteration
Another key departure betwixt ListIterator in addition to Iterator flat comes from the fact that y'all tin sack perform lot of change functioning using ListIterator e.g. y'all tin sack add together element, y'all tin sack alter chemical constituent in addition to y'all tin sack take chemical constituent using add(), set() in addition to remove() method, but y'all tin sack solely take objects using Iterator interface equally it solely got the remove() method. See chapter nineteen in addition to xx of Big Java: Early Objects past times Cay S. Horstmann to acquire to a greater extent than nearly how to add together elements spell iterating over ListIterator inward Java.

 The Iterator is the criterion agency to traverse a collection inward Java v Difference betwixt Iterator in addition to ListIterator inward Java?



Supported Collection classes
Another affair which differentiates Iterator in addition to ListIterator is that Iterator is supported past times most of the collection flat e.g. List, Set, Queue as good equally Map which is non a Collection but component of Java's Collection framework. Since Collection flat 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 past times calling the listIterator() method which is solely defined inward the List interface, thence it's solely available to concrete implementation of List interface e.g. ArrayList, LinkedList in addition to Vector class. You tin sack read Core Java for Impatient to acquire to a greater extent than nearly these classes in addition to Java Collection framework.


Iteration from whatever index
One of the unique features of List interface is that it provides closed to other overloaded listIterator(int index) method which tin sack live on used to strat traversal from whatever arbitrary index. Unfortunately, Iterator doesn't back upwardly this functionality because it has to operate over collection which doesn't back upwardly index-based access e.g. Set in addition to Queue interfaces. Anyway, You tin sack easily write an event of iterating over List starting at a special index inward Java using ListIterator.


Supported Methods
Last departure betwixt Iterator in addition to ListIterator inward this listing is structural. ListIterator seems to acquire to a greater extent than characteristic than Iterator thence got to a greater extent than methods e.g.
hasPrevious() to banking concern represent whether the listing has got to a greater extent than elements spell traversing the listing inward backward direction

next() which returns the side past times side chemical constituent inward the listing in addition to moves the cursor forward.

previous() which returns the previous chemical constituent inward the listing in addition to moves the cursor backward.

nextIndex() which returns the index of the side past times side chemical constituent inward the list.

previousIndex() which returns the index of the previous chemical constituent inward the list.

remove() to take the electrical flow chemical constituent inward the collection i.e chemical constituent returned past times next() or previous().  See how to take object using Iterator for to a greater extent than details.

set(E e) to supersede 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 inward the list


In short, hither is a overnice summary of differences betwixt ListIterator in addition to Iterator interface inward Java:

answer)
  • Difference between Hashtable in addition to HashMap inward Java? (answer)
  • Difference betwixt TreeMap in addition to TreeSet inward Java? (answer)
  • Difference between ArrayList in addition to LinkedList inward Java? (answer)
  • Difference betwixt HashMap in addition to LinkedHashMap inward Java? (answer)
  • Difference between EnumMap in addition to HashMap inward Java
  • Difference between ArrayList in addition to HashSet inward Java? (answer)
  • Difference betwixt HashSet in addition to TreeSet inward Java? (answer)
  • Difference between HashMap in addition to ConcurrentHashMap inward Java? (answer)
  • Difference betwixt Vector in addition to ArrayList inward Java? (answer)
  • Difference betwixt IdentityHashMap, WeakHashMap, in addition to EnumMap inward Java? (answer)

  • Further Learning
    Java In-Depth: Become a Complete Java Engineer
    Java Fundamentals: Collections
    Data Structures in addition to Algorithms: Deep Dive Using Java

    No comments:

    Post a Comment