Efficient intersection of two List<String> in Java?

Pentium10 picture Pentium10 · Mar 8, 2010 · Viewed 86.5k times · Source

Question is simple:

I have two List

List<String> columnsOld = DBUtils.GetColumns(db, TableName);
List<String> columnsNew = DBUtils.GetColumns(db, TableName);

And I need to get the intersection of these. Is there a quick way to achieve this?

Answer

Roman picture Roman · Mar 8, 2010

You can use retainAll method:

columnsOld.retainAll (columnsNew);