Removing objects from a list that don't appear in a list of ID's I have a list of objects with each of them having an ID. I have a second list that is just a list of media ID's. I want to remove the objects from the first list that don't match a mediaID contained in the second list. What is an efficient way of going about this? The only way I could think of was to have a nested for loop but that seems inefficient. 

Here is my implementation. Note that "items" is the first list, containing MediaDetailsItem objects, with each of these objects having a specific mediaId. The second list is mediaIds.

List&lt;MediaDetailsItem&gt; shortenedList = new ArrayList&lt;MediaDetailsItem&gt;();
                        for(MediaDetailsItem item : items){
                            for(String id : mediaIds){
                                if(item.getMediaId().equals(id)){
                                    shortenedList.add(item);
                                    break;
                                }
                            }
                        }
                        items.clear();
                        items.addAll(shortenedList);
You can do this without external iteration, if you are using java-8:

items.removeIf(item -&gt; !mediaIds.contains(item.getMediaId()));


It's not necessary to create a new list, this will modify the existing one.