• Post Reply Bookmark Topic Watch Topic
  • New Topic

using Iterator or without using Iterator  RSS feed

 
john wesley
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


just wondering what the difference is between these two versions of code ... anything to do with performance ??? or threads ???

----------------------------------

Iterator it = arrayList.iterator();

while (it.hasNext())
{
System.out.println(it.next());
}

-------------------------------

for (int i=0;i<arrayList.size();i++)
{
System.out.println(arrayList.get(i));
}



....jw
 
Sripathi Krishnamurthy
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

For 100 items in arraylist,
While->Iterator process took 10 milliseconds
for loop process took 0 milliseconds

For 1000 items in arraylist,
While->Iterator process took 40 milliseconds
for loop process took 40 milliseconds

For 10000 items in arraylist,
While->Iterator process took 260 milliseconds
for loop process took 220 milliseconds

For 100000 items in arraylist,
While->Iterator process took 1412 milliseconds
for loop process took 981 milliseconds

This is what I got when I tried this out. Can anyone explain why this is happenning?
[ March 28, 2005: Message edited by: Sripathi Krishnamurthy ]
 
K Riaz
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The whole point of an iterator is so that you can walk through any data structure which implements the interator interface without having to know exactly what that data structure is. This is useful when you need to return an iterator to a method/class from another method. You can change the data structure (say from an ArrayList to Vector if you want thread safe data structures) and your "client" class would not need to change the way it reads through the data structure, in this case, a Vector. If an iterator were not used, then the client would need to be changed from using an ArrayList (and its specific methods) to a Vector. An iterator provides great flexibility.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The for loop also has horrible performance with a LinkedList.

Additionally, the loop using the iterator will throw an Exception when during iterating the list gets modified (other than through the iterator). The for loop will "work", but with possibly strange results.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are curious why there's such a large time difference, just look at the code. Take advantage of having the source code for the core Java classes!Both the Iterator and for loop approach call get(index) and increment an index for iteration. The Iterator, however, involves the next() method call, the checkForComodification() method call, and a try-catch block (requires some stack setup, I believe).

That being said, there's much to be gained for code simplicity. And as Ilja said, using the for loop would cause a much bigger performance impact when you switched to LinkedList.
 
john wesley
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David Harkness:
If you are curious why there's such a large time difference, just look at the code. Take advantage of having the source code for the core Java classes!Both the Iterator and for loop approach call get(index) and increment an index for iteration. The Iterator, however, involves the next() method call, the checkForComodification() method call, and a try-catch block (requires some stack setup, I believe).

That being said, there's much to be gained for code simplicity. And as Ilja said, using the for loop would cause a much bigger performance impact when you switched to LinkedList.



dude, thanks so much.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!