Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Ron McLeod
• Paul Clapham
• Jeanne Boyarsky
• Bear Bibeault
Sheriffs:
• Rob Spoor
• Henry Wong
• Liutauras Vilda
Saloon Keepers:
• Tim Moores
• Carey Brown
• Stephan van Hulst
• Tim Holloway
• Piet Souris
Bartenders:
• Frits Walraven
• Himai Minh
• Jj Roberts

# Comparing 2 vectors, am I efficent?

Ranch Hand
Posts: 755
• Number of slices to send:
Optional 'thank-you' note:
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;
}
}
}
}

Ranch Hand
Posts: 280
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
you know what, I'll use your solution - it more elegant :-)
thanks

Amit Saini
Ranch Hand
Posts: 280
• Number of slices to send:
Optional 'thank-you' note:
glad i could be of any help

Ranch Hand
Posts: 82
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
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);

http://java.sun.com/docs/books/tutorial/collections/index.html

Peter Primrose
Ranch Hand
Posts: 755
• Number of slices to send:
Optional 'thank-you' note:
thanks, I'll try that. from what I've read it looks good.

thanks agine.

Bartender
Posts: 1843
• Number of slices to send:
Optional 'thank-you' note:
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 yam what I yam and that's all that I yam - the great philosopher Popeye. Tiny ad: SKIP - a book about connecting industrious people with elderly land owners https://coderanch.com/t/skip-book