Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Iterator and ListIterator.....

 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are the differences between these two 'Iterator'? & What about the ListIterator, Can't we use it for other types of collections, means Set, Queue etc.....

Thanks in Advanced!
 
Sheriff
Posts: 9695
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you see the documentation of Iterator and ListIterator?? The difference is clearly given there.

ListIterator wrote:An iterator for lists that allows the programmer to traverse the list in either direction, modify the list during iteration, and obtain the iterator's current position in the list.



Can't we use it for other types of collections, means Set, Queue


The name itself suggests that ListIterator is used to iterate over Lists. LinkedList is a Queue and you can use the ListIterator to iterate over LinkedList (as its also a List)...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit Garg, So, within the List objects, what are the differences of those two, the only difference is 'to traverse the list in either direction (ListIterator)'?

and one more question, What is meant by 'fail fast' regarding these two Iterators?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you look at the API documentation for Iterator and ListIterator, you'll see that ListIterator extends Iterator. So a ListIterator is an Iterator with some extra things added which are specifically for iterating lists, such as the ability to iterate in two directions (from the head to the tail, or from the tail to the head of the list).

Note that a List is an ordered collections, while other collections, for example Set, are not ordered. You can regard a List as a sequence of things, one after the other, and a Set as a bag of things which are not ordered in any way. Because of this, it makes sense for a List to be iterated in two directions, but not for a Set.

About your second question, you can find an answer here: What is a fail-fast iterator?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic