I'm having trouble getting intersection of multiple lists in Java. What I do is: I get (say) 3 Lists of integers:
list 1: [2, 2, 2, 2, 5, 5]
list 2: [2, 2, 103]
list 3: [2, 431]
I apply keepAll to the first, using each of the remaining lists:
list1.retainAll(list2);
list1.retainAll(list3);
And I get this result:
list1: [2, 2, 2, 2]
But I would expect to get this:
list1: [2]
... Since a single item has a common list, it is one 2, not four 2.
I know that this is probably the expected behavior of the keepAll function, but I need to get the result that I mentioned above.
Any help?
Edit:
Using a HashSet to prohibit duplicates will not help either. In this case, for example:
list 1: [2, 2, 2, 2, 5, 5]
list 2: [2, 2, 103]
list 3: [2, 2, 2, 431]
I need to get the result:
1: [2, 2] ( )
1: [2]