I’ve a class –
public class Data implements Identifiable{
private Integer id;
public Integer getId(){
return id;
}
}
now I’ve two collections-
List<Data> data1 = // few hundred Objects
Set<Integer> dataIds = // few object ids
I would like to extract the List<Data> from data1 which has ids in dataIds
How should be my approach? I’va guava in my classpath so can go with guava’s Functional approach if comparable in performance/efficiency .
Unless all you want to do is iterate through the result once or you need a reusable live filtered view, you probably want a non-view list containing the matches. Creating a
ListorSetto store the result and then iterating through the data list and adding matches is a perfectly good approach and easy to understand!I see your
Dataclass implements anIdentifiableinterface. Given that, you could create aFunction<Identifiable, Integer>that gets the ID…Identifiables.getIdFunction()or something. This is nice because it’d likely be useful in various other places (I talk about that approach in a blog post here). With that in place, doing this with Guava would be fairly simple as well:This is basically functionally equivalent to the first example, but seems like it’d be harder to understand. Since there isn’t any clear benefit to doing this (unlike in a situation where you want to just use the live view), my recommendation would be to just go with the first.