Click to See Complete Forum and Search --> : comparing vectors in java
arvin84
06-23-2007, 02:28 AM
hi,
iam having vector v1 and v2 of diff size,i need compare v1 and v2 and find unique records from them
pls help
arvin
Khalid Ali
06-23-2007, 12:50 PM
you will have to write your own comparison routine to do that, I don't think there is in java that will automatically tell you that.
there are few things that come to mind,
1. first see if both vectors are of the same size.
2. then scan through both and compare each item at the same index.
3. You will have to know in advance that what is the type of each item in the vector so that you can compare correctly..
just my 0.001 cent
cst97025
07-01-2007, 05:48 PM
You can probably use Commons-Collections to do that.
Look at CollectionUtils.retainAll().
http://jakarta.apache.org/commons/collections/api-release/org/apache/commons/collections/CollectionUtils.html#retainAll(java.util.Collection,%20java.util.Collection)
This assumes that your Vectors contain all the same type of object. If it's your own object, you should probably make sure you code its equals() method.
At least I'm pretty sure it will work with Vectors...
If not, maybe you can switch them to ArrayLists.