This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparing 2 vectors, am I efficent?

 
Peter Primrose
Ranch Hand
Posts: 755
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there a better (more efficent way) to compate 2 vectors then the solution below?
this is the problem: I have a vector A with names [say 100 names] and another vector B with 8 names (all of them are subset of vector V)

i need to know WHERE are they located on vector A (the way they are ordered is very importent) so in other words if "James" is found in vector B I want to know what is its index in vector A.

Thanks

A=v
B=recordVector


int [] index = new int[100];
for (int i=0; i<100; i++)
{
index[i]= -1 ;
}

if( ! recordVector.isEmpty() )
{
for (int x=0; x<recordVector.size(); x++)
{
for (int i=0; i<v.size(); i++)
{
if (v.get(i).toString().equals(recordVector.get(x).toString()))
{
index[x]=i;
}
}
}
}
 
Amit Saini
Ranch Hand
Posts: 280
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cant you do something like this:

1.loop over vector B with 8 elements
2. check if A 'contains()' element from vector B
3. if yes, return 'indexOf()' element in A
4. if no, just proceed with the loop.

Does it make sense? I hope Im understanding your problem correctly !
 
Peter Primrose
Ranch Hand
Posts: 755
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, your answer makes sense, however, I'm not sure it will be more efficent.
I think that you have the same solution as O(N*M)

what do you think?
 
Amit Saini
Ranch Hand
Posts: 280
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it will be more efficient.

In your code, you loop over B first. (8 elements)
After that you do another for loop and loop over A (100 elements) and compare each element of A to B to check if they are equal.

In my solution, the inner for loop is avoided and is instead replaced by standard Vector API, contains() and indexOf(). Now I dont know what internal algorithm is used for contains() and indexOf().

But yeah, we're doing almost the same thing, I agree.
 
Peter Primrose
Ranch Hand
Posts: 755
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you know what, I'll use your solution - it more elegant :-)
thanks
 
Amit Saini
Ranch Hand
Posts: 280
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
glad i could be of any help
 
rahul V kumar
Ranch Hand
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was thinking why not create a hashmap of your main vector A.

Then all you have to do is just loop through your vector B and look in that hashmap.
 
Peter Primrose
Ranch Hand
Posts: 755
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, sometimes I might add or MOVE items to the vector , and the oreder I enter them is important (that's my index). I suspect that hash will not help me as the order in which you enter them is important (mostly when you have a collision).
in the vector case I can simple 'null' the element.

Do you think my observation is correct?
 
rahul V kumar
Ranch Hand
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why dont you use arraylists instead of vector.
you will find better performance with arraylist than vector.
[ October 21, 2004: Message edited by: rahul V kumar ]
 
rahul V kumar
Ranch Hand
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rahul V kumar:
I was thinking why not create a hashmap.

Then all you have to do is just loop through your vector B and look in that hashmap.
 
Amit Saini
Ranch Hand
Posts: 280
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yups. I concur with Rahul. Vector methods are serialized and so its a performance hit. ArrayLists dont have serialized methods and so are supposed to be faster. How much of a difference will it make to your application, i dont know! So if you dont need synchronization, ArrayList is a good choice. Even if you do need synchronization with ArrayLIst, you could use this function, public static List synchronizedList(List list);

Refer to this link for more info.
http://java.sun.com/docs/books/tutorial/collections/index.html
 
Peter Primrose
Ranch Hand
Posts: 755
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, I'll try that. from what I've read it looks good.

thanks agine.
 
Joel McNary
Bartender
Posts: 1824
Eclipse IDE Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amitdsaini :

Welcometo JavaRanch! Please help us abide by our naming convention by adding a few spaces to your display name here. (We are looking for both a first and a last name, separated by spaces, in the display names on JavaRanch.

Thanks!

Joel
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic