Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

有这样一个模型类

public class User{
    private Integer userId;
    private String userName;
    // getter、setter 就不写了
}

通过查询SQL获取到该对象的 List 类型的数据 - 数量未知
我想要把 userId 构建为一个 Integer 数组,要如何优雅、高效的做?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
498 views
Welcome To Ask or Share your Answers For Others

1 Answer


List<Integer> ids = users.stream().map(User::getUserId).collect(Collectors.toList());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share
...