File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Find the size of Iterator thing

 
Max Bean
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

is this such a crappy way to find how many items in the Iterator?

Many thnx.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if all you have is an Iterator, and you want to know how many items there are, the only way to do it is to go through each item. On the other hand, Iterators often come from Collections of some sort - if that's the case, and you want to know the size, use the size() method on the Collection, not the Iterator. Iterators aren't really designed for anything other than, well, iterating.
 
Peter Chase
Ranch Hand
Posts: 1970
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's important to understand that the absence of a size() method in Iterator is not an accidental omission. It's very deliberate.

Some things that can be iterated don't know how many items there are. As a possibly-poor example, one could consider implementing an Iterator that returned lines typed at the console by the user, with some special keystroke being used to indicate end-of-data. Such an Iterator couldn't possibly know how many lines the user was going to type.

Other things that can be iterated can only find the number of items by a very expensive operation. A classic example is LinkedList. A LinkedList can be iterated very efficiently, but getting its size involves traversing the whole list.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12020
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would think an iterator doesn't need to know how many things there are. that's not it's job. it is supposed to know how to get the next, maybe get the previous, is there another one, etc. it doesn't need knowledge of the size of what it's iterating over.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Peter Chase]: A LinkedList can be iterated very efficiently, but getting its size involves traversing the whole list.

That's true for a typical linked list implementation, but not for a java.util.LinkedList. LinkedList maintains size as an instance field, and updates it whenever an element is added or removed. So getting the size at any moment is very easy. I think that most of the Java library collections work similarly. But the general point is valid - it's possible for some collections to have very slow or inefficient size() methods.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic