I have two ArrayList<Long> with huge size about 5,00,000 in each. I have tried using for loop which usage list.contains(object), but it takes too much time. I have tried by splitting one list and comparing in multiple threads but no effective result found.
I need the no. of elements that are same in both list.
Any optimized way?
Have you considered putting you elements into a
HashSetinstead? This would make the lookups much faster. This would of course only work if you don’t have duplicates.If you have duplicates you could construct
HashMapthat has the value as the key and the count as the value.