可以使用Java 8 Stream API来实现这一功能。例如:
List<Person> people = ...;
List<Integer> idList = people.stream()
.map(Person::getId)
.collect(Collectors.toList());
在这里,people
是一个包含 Person
对象的列表,每个 Person
对象都有一个 id
属性。通过调用 stream()
方法,你可以创建一个流,然后使用 map()
方法对流中的每个元素调用 getId()
方法,这样就可