Is it possible to create the below function into a generic function that I can re-use?
I want to pass in a collection, and return a list of a particular property in the object.
So far example I have this:
private List<Integer> getPropertyABCIdsFromSomeObject(List<SomeObject> someObjects) {
List<Integer> ids = new ArrayList<Integer>();
for(SomeObject so : someObjects) {
ids.add(so.getPropertyABCId());
}
return ids;
}
So this method works only for type SomeObject, and returns a list of PropertyABCId.
Now if I want a list of some other property, I have to replicate this function with very little that changes.
Can generics save the day here?
You would need an interface, but yes: