首页 > 编程语言 >取两个List<Object>的差集数据和交集数据 java

取两个List<Object>的差集数据和交集数据 java

时间:2022-08-15 15:35:06浏览次数:61  
标签:newProjectList stream projectSimpleInfoVOList Object List 差集 projectSimpleInfoVO

List<ProjectSimpleInfoVO> projectSimpleInfoVOList = new ArrayList<>();
        ProjectSimpleInfoVO projectSimpleInfoVO = new ProjectSimpleInfoVO();
        projectSimpleInfoVO.setProjectId("11");
        projectSimpleInfoVO.setProjectName("haha");
        ProjectSimpleInfoVO projectSimpleInfoVO1 = new ProjectSimpleInfoVO();
        projectSimpleInfoVO1.setProjectId("22");
        projectSimpleInfoVO1.setProjectName("hehe");
        projectSimpleInfoVOList.add(projectSimpleInfoVO);
        projectSimpleInfoVOList.add(projectSimpleInfoVO1);

        List<ProjectSimpleInfoVO> projectSimpleInfoVOList1 = new ArrayList<>();
        ProjectSimpleInfoVO projectSimpleInfoVO2 = new ProjectSimpleInfoVO();
        projectSimpleInfoVO2.setProjectId("22");
        projectSimpleInfoVO2.setProjectName("hehe");
        projectSimpleInfoVOList1.add(projectSimpleInfoVO2);

        //取 projectSimpleInfoVOList1 存在 projectSimpleInfoVOList 不存在的差集
        List<ProjectSimpleInfoVO> newProjectList = projectSimpleInfoVOList1.stream()
                .filter(item -> !projectSimpleInfoVOList.stream()
                        .map(ProjectSimpleInfoVO::getProjectId)
                        .collect(Collectors.toList())
                        .contains(item.getProjectId()))
                .collect(Collectors.toList());
        System.out.println(newProjectList);
     结果:[]
//取projectSimpleInfoVOList 存在 projectSimpleInfoVOList1 不存在的差集 List<ProjectSimpleInfoVO> newProjectList = projectSimpleInfoVOList.stream() .filter(item -> !projectSimpleInfoVOList1.stream() .map(ProjectSimpleInfoVO::getProjectId) .collect(Collectors.toList()) .contains(item.getProjectId())) .collect(Collectors.toList()); System.out.println(newProjectList);
    结果:[ProjectSimpleInfoVO(projectId=1, projectName=haha)] //取projectSimpleInfoVOList 存在 projectSimpleInfoVOList1 都有的数据交集 List<ProjectSimpleInfoVO> newProjectList = projectSimpleInfoVOList.stream() .filter(item -> projectSimpleInfoVOList1.stream() .map(ProjectSimpleInfoVO::getProjectId) .collect(Collectors.toList()) .contains(item.getProjectId())) .collect(Collectors.toList()); System.out.println(newProjectList);
      结果:[ProjectSimpleInfoVO(projectId=22, projectName=hehe)]

 

 

标签:newProjectList,stream,projectSimpleInfoVOList,Object,List,差集,projectSimpleInfoVO
From: https://www.cnblogs.com/henrychenhao/p/16588468.html

相关文章