【笔记】从List中取出指定字段等于指定值的对象

本文探讨了两种在Java中根据ID从Student集合中获取对象的方法:将集合转换为Map和使用Stream的filter。对于小数据量,filter方法更清晰,但大数据量下,构建Map以提高检索效率更优。性能测试显示,Map方案的平均运行时间远低于filter。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

例如我们有一个Student集合,每个对象有“id”,“name”,“age”三个字段,如下:
@Data
@AllArgsConstructor
public class Student {

    private Integer id;

    private String name;

    private Integer age;
}
其中id字段是1000~1099,如下:
Student(id=1000, name=学生_0000, age=18)
Student(id=1001, name=学生_0001, age=22)
Student(id=1002, name=学生_0002, age=27)
……
……
需求:我们需要根据id=xxxx来取出Student对象。

方案一

根据id字段,把List转换为Map。
//注意:该方法要求id字段的值是唯一的 否则需要使用下面的方法转Map
Map<Integer, Student> studentMap = studentList.stream().collect(Collectors.toMap(Student::getId, Function.identity()));

//根据id字段分组,且分组之后的对象集合取第一个对象
Map<Integer, Student> studentMap = studentList.stream().collect(Collectors.groupingBy(Student::getId, Collectors.collectingAndThen(Collectors.toList(), students -> students.get(0))));
需要取哪个对象,直接从map中取出即可。

方案二

使用stream的filter进行过滤。
Student student = studentList.stream().filter(s -> Objects.equals(s.getId(), id)).findFirst().orElse(null);
在filter中过滤出符合条件的对象,不匹配返回null。
实际使用中,数据量都不会特别大,个人认为方案二更清晰一些。
如果数据量较大,且需要从集合中多次取出,方案一的效率更高。因为方案一只聚合一次,而方案二需要反复创建stream并过滤集合。

性能测试

创建100个对象的测试集合,循环取值1000000次。
public class Test {

    public static void main(String[] args) {
        //创建集合
        List<Student> studentList = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            studentList.add(new Student(1000 + i, "学生_" + String.format("%04d", i), randomInt(18, 30)));
        }
        //分组测试
        long mapTs = System.currentTimeMillis();
        Map<Integer, Student> studentMap = studentList.stream().collect(Collectors.toMap(Student::getId, Function.identity()));
        for (int i = 0; i < 1000000; i++) {
            int id = randomInt(1000, 1100);
            Student student = studentMap.get(id);
        }
        System.out.println("分组测试: " + (System.currentTimeMillis() - mapTs) + "ms");
        //过滤测试
        long filterTs = System.currentTimeMillis();
        for (int i = 0; i < 1000000; i++) {
            int id = randomInt(1000, 1100);
            Student student = studentList.stream().filter(s -> Objects.equals(s.getId(), id)).findFirst().orElse(null);
        }
        System.out.println("过滤测试: " + (System.currentTimeMillis() - filterTs) + "ms");
    }
}
测试五次的结果:
分组测试: 34ms
过滤测试: 500ms
-----
分组测试: 34ms
过滤测试: 503ms
-----
分组测试: 32ms
过滤测试: 511ms
-----
分组测试: 33ms
过滤测试: 511ms
-----
分组测试: 31ms
过滤测试: 491ms
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值