Pentium10 Pentium10 - 2 years ago 64
Java Question

Efficient intersection of two List<String> in Java?

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 Source

You can use retainAll method:

columnsOld.retainAll (columnsNew);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download