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<MediaDetailsItem> shortenedList = new ArrayList<MediaDetailsItem>();
for(MediaDetailsItem item : items){
for(String id : mediaIds){
if(item.getMediaId().equals(id)){
shortenedList.add(item);
break;
}
}
}
items.clear();
items.addAll(shortenedList);