Find all numbers that appear in each of a set of lists

Posted by Ankur on Stack Overflow See other posts from Stack Overflow or by Ankur
Published on 2010-05-04T13:05:32Z Indexed on 2010/05/04 13:08 UTC
Read the original article Hit count: 183

Filed under:
|
|
|

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:

  1. 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
  2. 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.

© Stack Overflow or respective owner

Related posts about java

Related posts about arraylist