In C# I can do this:
IEnumerable<long> ids = things.select(x => x.Id);
In Java I have to do this:
Collection<Long> ids = new ArrayList<Long>(things.size());
for(Thing x : things)
ids.add(x.getId());
Have to do this sort of thing quite a lot now and wonder if there is a more generic way to do this in Java. Could create a method to do it, but then I would have to add an interface with the getId method or something like that… which I can’t…
using Guava, specifically the function interface :
and invoked like this (where transform is a static import from Collections2 of guava:
Guava has quite a few other benefits too.