记录java常用方法

记录java常用方法

1.取两个集合的差集

java 复制代码
public class StreamApiTest {

    public static void main(String[] args) {
        List<MktCamCustDetail> batchPos = Arrays.asList(
                new MktCamCustDetail("123456", 123456L, "张三"),
                new MktCamCustDetail("345678", 345678L, "王五"),
                new MktCamCustDetail("234567", 234567L, "赵六"),
                new MktCamCustDetail("123456", 123456L, "张三"));
        // 从 batchPos 中提取 objId 列表,去重后直接用于查询
        List<MktCamCustDetail> mktCamCustDetailList = Optional.ofNullable(batchPos)
                .orElse(Collections.emptyList()) // 防止 batchPos 为 null
                .stream()
                .map(MktCamCustDetail::getObjId)
                .filter(Objects::nonNull) // 避免包含 null 的 objId
                .distinct()
                .collect(Collectors.collectingAndThen(Collectors.toList(), StreamApiTest::getMktCamCustDetails));

        // 取读取到的数据与数据库中已存在的资产的差集,即新增的资产
        List<MktCamCustDetail> newMktCamCustDetailList = batchPos.stream()
                .filter(m -> mktCamCustDetailList.stream().noneMatch(m2 -> m2.getObjId().equals(m.getObjId())))
                .collect(Collectors.toList());
        System.out.println(newMktCamCustDetailList);
    }

    /**
     * 根据 objIdList 查询 MktCamCustDetail 列表
     *
     * @param objIdList
     * @return
     */
    public static List<MktCamCustDetail> getMktCamCustDetails(List<String> objIdList) {
        List<MktCamCustDetail> list = Arrays.asList(
                new MktCamCustDetail("123456", 123456L, "张三"),
                new MktCamCustDetail("345678", 345678L, "王五"),
                // new MktCamCustDetail("234567", 234567L, "赵六"),
                new MktCamCustDetail("123456", 123456L, "张三")
        );
        // 如果 objIdList 为空,直接返回空列表,避免不必要的查询
        return list;
    }
}

/**
 * MktCamCustDetail 实体类
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
class MktCamCustDetail {
    private String objId;
    private Long custId;
    private String custName;
}
相关推荐
学长毕设5 小时前
基于SpringBoot的实验室资产管理系统
java·spring boot·后端
C^h6 小时前
c++ string
开发语言·c++
raindayinrain6 小时前
c++高性能--原子操作,无锁编程,操作顺序
开发语言·c++·性能优化
学逆向的6 小时前
C++ 合并命名空间
开发语言·c++·网络安全
Zhao-Jichao6 小时前
VS2026 打包 C++ 程序生成 EXE,借助 Installer Project 扩展工具
开发语言·c++
zzu123zsw7 小时前
从臃肿的模板调用到声明式契约:深入 UniHttp 动态代理与多渠道生命周期扩展
java·spring boot·microsoft·unihttp
学逆向的7 小时前
C 局部静态变量
c语言·开发语言·网络安全
大都督会赢的7 小时前
C++第一节课
开发语言·c++
“AI国潮设计-小江”7 小时前
【AIGC实战】Python+Stable Diffusion打造“英歌舞财神”IP,解锁“马到成功”等爆款情绪营销文案设计
开发语言·人工智能·python·prompt·aigc
源码集结号8 小时前
云诊所门诊系统源码,基于SpringBoot+Vue.js架构,支持多租户设计与Docker容器化部署
java·spring boot·源码·医院信息系统·门诊系统·诊所系统