joe weakers

Ranch Hand

Posts: 38

posted 12 years ago

Hi there. My problem is as follows:

The set X is composed of 30 unique elements. I have three vectors, A, B, and C, that hold unique elements from X. In other words A,B, and C are all subsets of set X. The size of A ranges from 1-12 and cannot be 0. The size of b ranges from 0-12 and can be empty. The size of C ranges from 1-12 and cannot be 0. A,B, and C all contain unique elements.

What is the best way (optimal solution) to finding the intersection of A and B and C? Is using vectors not a good option - there are so few elements in each vector does it really make any difference? I was going to rely on the contains(obj) method that is provided by java.util.Vector. Is this too slow to be using?

I should also state that if an element is in A then it is extremely likely to be either B or C or both. I know the query is vague but any help would be appreciated nonetheless. thnaks a lot, Joe.

The set X is composed of 30 unique elements. I have three vectors, A, B, and C, that hold unique elements from X. In other words A,B, and C are all subsets of set X. The size of A ranges from 1-12 and cannot be 0. The size of b ranges from 0-12 and can be empty. The size of C ranges from 1-12 and cannot be 0. A,B, and C all contain unique elements.

What is the best way (optimal solution) to finding the intersection of A and B and C? Is using vectors not a good option - there are so few elements in each vector does it really make any difference? I was going to rely on the contains(obj) method that is provided by java.util.Vector. Is this too slow to be using?

I should also state that if an element is in A then it is extremely likely to be either B or C or both. I know the query is vague but any help would be appreciated nonetheless. thnaks a lot, Joe.

Ilja Preuss

author

Sheriff

Sheriff

Posts: 14112

posted 12 years ago

Well, the *easiest* solution would be to clone A and use retainAll on the clone with both B and C as arguments. After that, the clone contains only elements that are contained in all three original Vectors.

For *big* sets (say, >10000 elements), it's likely that using HashSets instead of Vectors is significantly faster, *if* the elements implement hashcode() in a reasonable way. For your small number of elements, I don't think it would make a measurable difference. In some cases it might in fact be slower. You will have to try to be sure, though.

For *big* sets (say, >10000 elements), it's likely that using HashSets instead of Vectors is significantly faster, *if* the elements implement hashcode() in a reasonable way. For your small number of elements, I don't think it would make a measurable difference. In some cases it might in fact be slower. You will have to try to be sure, though.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus