Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.1k views
in Technique[技术] by (71.8m points)

Java中优雅、高效的将引用类型对象构成的 List 分字段转数组

有这样一个模型类

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

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


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

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...