I'm having troubles to get the intersection of several Lists on Java. What I'm doing is this: I get (lets say) 3 Lists of integer numbers:
list 1: [2, 2, 2, 2, 5, 5]
list 2: [2, 2, 103]
list 3: [2, 431]
I'm applying retainAll to the first one using each of the remaining lists:
list1.retainAll(list2);
list1.retainAll(list3);
And I'm getting this result:
list1: [2, 2, 2, 2]
But I'd expect to get this one:
list1: [2]
...Since the only element all lists share is one 2 and not four 2.
I know this is probably the expected behaviour of the retainAll function, but I need to get the result I mentioned above.
Any help?
Edit: Using a HashSet to disallow duplicates won't do the trick either. In this case, for instance:
list 1: [2, 2, 2, 2, 5, 5]
list 2: [2, 2, 103]
list 3: [2, 2, 2, 431]
I need to get a result of:
list 1: [2, 2] (since all lists have at least a pair of 2's)
Instead of
list 1: [2]