I have a collection with two object types. I want to only read one of the two types into a new Set. Is there an elegant way of doing this?
Use Google Guava's filter.
Collections2.filter(yourOriginalCollection, new Predicate<Object>() {
public boolean apply(Object obj) {
return obj instanceof TypeYouAreInterestedIn;
}
});
Or in Java 8:
Collections2.filter(yourOriginalCollection, (obj) -> obj instanceof TypeYouAreInterestedIn);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With