Skip to content
Advertisement

Intersection and union of ArrayLists in Java

Are there any methods to do so? I was looking but couldn’t find any.

Another question: I need these methods so I can filter files. Some are AND filters and some are OR filters (like in set theory), so I need to filter according to all files and the unite/intersects ArrayLists that holds those files.

Should I use a different data structure to hold the files? Is there anything else that would offer a better runtime?

Advertisement

Answer

Here’s a plain implementation without using any third-party library. Main advantage over retainAll, removeAll and addAll is that these methods don’t modify the original lists input to the methods.

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement