Find all numbers that appear in each of a set of lists
- by Ankur
I have several ArrayLists of Integer objects, stored in a HashMap.
I want to get a list (ArrayList) of all the numbers (Integer objects) that appear in each list.
My thinking so far is:
Iterate through each ArrayList and put all the values into a HashSet
This will give us a "listing" of all the values in the lists, but only once
Iterate through the HashSet
2.1 With each iteration perform ArrayList.contains()
2.2 If none of the ArrayLists return false for the operation add the number to a "master list" which contains all the final values.
If you can come up with something faster or more efficient, funny thing is as I wrote this I came up with a reasonably good solution. But I'll still post it just in case it is useful for someone else.
But of course if you have a better way please do let me know.