Why in Groovy, when I create 2 lists, is there a difference if I do a.intersect( b ) and b.intersect( a ):
def list1 = ["hello", "world", "world"];
def list2 = ["world", "world", "world"];
println( "Intersect list1 with list2: " + list1.intersect( list2 ) );
println( "Intersect list2 with list1: " + list2.intersect( list1) );
traces:
Intersect list1 with list2: [world, world, world]
Intersect list2 with list1: [world, world]
(you can copy it here: http://groovyconsole.appspot.com/ if you want to test it)
If the arrays all contain unique elements, then it works as normal. Once you begin to add duplicates, it gets weird:
def list1 = ["hello", "world", "test", "test"];
def list2 = ["world", "world", "world", "test"];
println( "Intersect list1 with list2: " + list1.intersect( list2 ) );
println( "Intersect list2 with list1: " + list2.intersect( list1 ) );
traces:
Intersect list1 with list2: [world, world, world, test]
Intersect list2 with list1: [world, test, test]
I thought the whole point of intersect() was to give you the common elements, so it didn’t matter which order you put them in?
If this isn’t the case, how can I get only the common elements (expect duplicates in the array). E.g. example one should return ["world", "world"] and example two should return ["world", "test"]
Edit
To clarify a bit, this code should test that user data is still the same (assuming they disconnected in the middle of something and we want to make sure the data hasn’t been tampered with, or is in the same state as before).
The order of the lists can’t be guaranteed (the user could reorder it, but it’s still technically the “same”), and duplicates are possible.
So something like: ["one", "one", "two"] should match ["two", "one", "one"], whereas any addition to the lists, or change in data shouldn’t match.
If you look at the source for
Collection.intersect, you can see that the logic of the method follows this flow:for two collections,
leftandrightleftandrightifleftis smaller thanrightleftinto a Set (removes duplicates)rightif it exists in theleftSet, then add it to the resultsSo, for your last 2 examples;
array1.intersect( array2 )would give (if we wrote that same algorithm in Groovy):Which (if you run it), you can see means result has the value
[world, world, world, test](as you found). This is because every element inrightcan be found in theleftSetNot sure why the first example should return
["world","world"]though…later…
So, what I think you are looking for would be something like this:
in order that you cope with the duplicates in the collections, as then both
intersect1andintersect2will be equal tolater still
I believe this does what you want: