Java8用一行代码,抽取List对象中的id到新List中

示例代码:

List<User> users = getUserList();
List<Integer> userIds = users.stream().map(User::getId).collect(Collectors.toList());

以上就是从用户对象集合中抽取出用户对象的id到一个新的List中,参数对号入座即可,同样的,也不一定是要抽取id,其他的属性也是可以的。

猜你喜欢

转载自blog.csdn.net/weixin_38106322/article/details/111600544