With Guava you can use Function like -
private enum StudentToId implements Function<Student, Integer> {
INSTANCE;
@Override
public Integer apply(Student input) {
return input.getId();
}
}
and you can use this function to convert List of students to ids like -
Lists.transform(studentList, StudentToId.INSTANCE);
Surely it will loop in order to extract all ids, but remember guava methods returns view and Function will only be applied when you try to iterate over the List<Integer>
If you don't iterate, it will never apply the loop.
Note: Remember this is the view and if you want to iterate multiple times it will be better to copy the content in some other List<Integer>
like
ImmutableList.copyOf(Iterables.transform(students, StudentToId.INSTANCE));