Java比较两个list,A中有但B中没有的元素

复制代码
一、比较两个list的差异
public List<String> getAList( List<String> listA ,List<String> listB){
    // 使用Stream过滤
    List<String> result = listA.stream()
            .filter(element -> !listB.contains(element))
            .collect(Collectors.toList());
    log.info("A中有但B中没有的元素:{} ",result);
    return result;
}
java 复制代码
public List<String> getAList( List<String> listA ,List<String> listB){
    // 使用Stream过滤
    List<String> result = listA.stream()
            .filter(element -> !listB.contains(element))
            .collect(Collectors.toList());
    log.info("A中有但B中没有的元素:{} ",result);
    return result;
}

二、计算一个list里面的值的总和

复制代码
//计算总工时
int totalHjValue = PmMemberHourVoList.stream()
        .map(PmMemberHourVo::getHjValue)                    // 提取工时
        .filter(hjValue -> hjValue != null && hjValue.matches("\\d+"))  // 过滤有效数字
        .mapToInt(Integer::parseInt)          // 转换为int
        .sum();
java 复制代码
 //计算总工时
        int totalHjValue = PmMemberHourVoList.stream()
                .map(PmMemberHourVo::getHjValue)                    // 提取工时
                .filter(hjValue -> hjValue != null && hjValue.matches("\\d+"))  // 过滤有效数字
                .mapToInt(Integer::parseInt)          // 转换为int
                .sum();

Stream(流)是 Java 8 引入的用于处理集合数据的高级抽象,支持函数式编程风格的操作。

  • 声明式:描述做什么,而不是怎么做

  • 可组合:多个操作可以链式调用

  • 可并行:支持并行处理提高性能

  • 内部迭代:自动迭代,无需手动写循环

相关推荐
拾光Ծ几秒前
【Linux网络】网络通信实战:UDP & TCP Socket编程实现Echo Server
linux·网络·网络协议·tcp/ip·udp·线程池
sukalot15 分钟前
windows网络适配器驱动开发-为多个 Windows 版本生成 WiFiCx 驱动程序
windows·驱动开发
翰德恩咨询21 分钟前
华为DSTE咨询洞察:如何规划人才支撑战略落地
运维·服务器·华为
一叶龙洲11 小时前
wslg打开Ubuntu24.04默认打开图形界面
linux·服务器·数据库·ubuntu
CCPC不拿奖不改名11 小时前
大模型推理架构与开源生态知识整理
数据库·windows·python·架构·langchain·开源·github
敖行客 Allthinker13 小时前
Parallels Ubuntu虚拟机项目如何让手机访问?完整解决方案
linux·运维·ubuntu
王维同学14 小时前
[自学][Windows C++]RunOnceEx 注册表分组键的安全遍历
c++·windows·安全·开源
keyipatience14 小时前
线程栈与TLS和线程互斥
java·linux·服务器·开发语言·ubuntu
j7~14 小时前
【Linux】二十二.《Linux 信号机制完全指南:表示、捕捉与处理》
linux·信号处理·volatile·可入重函数·保存信号
踏月的造梦星球15 小时前
达梦数据库执行计划与性能分析入门:EXPLAIN、AUTOTRACE 与 ET
服务器·数据库·oracle