Is there a nice way in Java to read a parameter from an object in a list? For example I often want to get all ids in my objects that are part of a list. The code I usually write for this is as below.
List<Integer> ids = new ArrayList<Integer>();
for (MyObject myObj : myObjs){
ids.add(myObj.getId());
}
Is there any other way of doing this or any other pattern? Or am I just waiting for (proper) closures to appear in java. All I want is a one-line to return a list of all the values of one parameter of a list.
Thanks
Guava
Lists.transform:EDIT: you can also use a custom
ToIdclass implementingFunction<F, T>