• Post Reply Bookmark Topic Watch Topic
  • New Topic

Performance Comparision  RSS feed

 
santosh batta
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I want to know, how the Iterator works in 2 cases.

Case 1: Say I've list of items..

List list = new ArrayList();

list.add(item1);
list.add(item2);
list.add(item3);
list.add(item4);...

Iterator itr = list.iterator();

Case 2: Now, I've another list of items added to vector..

Vector vtr = new Vector();

vtr.add(item1);
vtr.add(item2);
vtr.add(item3);
vtr.add(item4);...

List list = vtr;

Iterator itr = list.iterator();

Now, I'll iterate through each item & process it. I want to know, if there is any performance degradation for case 2. Could you please suggest? It may sound stupid to assign vector to list(in case2), but I can't avoid it. Can you please suggest, if that kills the performance?
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Santosh, you're only copying the reference to the Vector to a new variable. The iterator you're getting is the exact same you would get if you just performed vtr.iterator().

I would be much more worried about other things. Why are you using Vector at all? The class is deprecated.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
santosh batta wrote:Now, I'll iterate through each item & process it. I want to know, if there is any performance degradation for case 2.

Yes, but it has nothing to do with what you're doing, and everything to do with the fact that Vector is synchronized, whereas ArrayList isn't. Vector is also a legacy class, and so possibly hasn't been "tuned" as much to keep up with all the changes to the Java memory model (but I honestly don't know).

Could you please suggest? It may sound stupid to assign vector to list(in case2), but I can't avoid it.

Well, if you can't avoid it, doesn't that make the question moot?

It might also be worth pointing out that since Vector now implements List, you can simply write:
List list = new Vector();
and leave everything else the same.

Winston
 
santosh batta
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Santosh, you're only copying the reference to the Vector to a new variable. The iterator you're getting is the exact same you would get if you just performed vtr.iterator().

I would be much more worried about other things. Why are you using Vector at all? The class is deprecated.


Thanks Stephan for your response. I don't intended to use vector. But its a third party lib that uses vector & I can't change it.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
santosh batta wrote:Thanks Stephan for your response. I don't intended to use vector. But its a third party lib that uses vector & I can't change it.

In which case, I strongly suggest that you use List, not Vector, because Vectors are Lists; and that way you don't tie yourself down to their implementation.

Winston
 
Rafael Prado Oliveira
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I would do is to use list, and have some methods to transform it from / to vector, in order to the other third party methods to handle it.

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!