比较两个list对象中的某个值是否相等【没有ID情况】 全部数据对比
public static void main(String[] args) {
//list1 第一次导入的数据
List<EamData> list1 = new ArrayList<>();
EamData eamData1 = new EamData();
eamData1.setId("1");
eamData1.setPersonName("李文");
eamData1.setApproverName("张超 20096684");
eamData1.setTaskStatus("已关闭");
EamData eamData2 = new EamData();
eamData1.setId("2");
eamData2.setPersonName("李全成");
eamData2.setApproverName("张超 20096684");
eamData2.setTaskStatus("已关闭");
list1.add(eamData1);
list1.add(eamData2);
//list1 第二次导入的数据
List<EamData> list2 = new ArrayList<>();
EamData eamData3 = new EamData();
eamData3.setPersonName("李全成");
eamData3.setApproverName("张超 20096684");
eamData3.setTaskStatus("已关闭");
EamData eamData4 = new EamData();
eamData4.setPersonName("WHY");
eamData4.setApproverName("张超 20096684");
eamData4.setTaskStatus("已关闭");
list2.add(eamData3);
list2.add(eamData4);
List<EamData> eamDataList = getDifferent(list1,list2);
//过滤有ID的数据
List<EamData> boys = eamDataList.stream().filter(s->s.getId() == null).collect(Collectors.toList());
for(EamData item:boys){
System.out.println("过滤后:"+item.getId()+"\t"+item.getPersonName()+"\t"+item.getApproverName());
}
比较两个list对象取不相等的交集
private static List<EamData> getDifferent(List<EamData> prelist, List<EamData> curlist) {
List<EamData> list = new ArrayList<EamData>();
Map<EamData,Integer> map = new HashMap<>(curlist.size());
for (EamData item : curlist) {
map.put(item, 1);
}
for (EamData item : prelist) {
if(map.get(item)!=null)
{
map.put(item, 2);
continue;
}
list.add(item);
}
for(Map.Entry<EamData, Integer> entry:map.entrySet())
{
if(entry.getValue()==1)
{
list.add(entry.getKey());
}
}
for(EamData item:list){
System.out.println("2个List<对象>不相等的交集:"+item.getId()+"\t"+item.getPersonName()+"\t"+item.getApproverName());
}
return list;
}
实体类
@Data
public class EamData {
private String id;
private String personName;
private String approverName;
private String taskStatus;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
EamData eamData = (EamData) o;
return Objects.equals(id, eamData.id) &&
Objects.equals(personName, eamData.personName) &&
Objects.equals(approverName, eamData.approverName) &&
Objects.equals(taskStatus, eamData.taskStatus);
}
}
结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d010fc609bdb817ae46437912b1f9965.png)
升级版【stream().filter】
Set<EamData> list1Set = new HashSet<>(list1);
Set<EamData> list2Set = new HashSet<>(list2);
// 交集
List<EamData> intersection = list1.stream().filter(list2Set::contains).collect(Collectors.toList());
System.out.println("---得到交集 intersection---");
intersection.parallelStream().forEach(System.out::println);
// 差集 (list1 - list2)
List<EamData> reduce1 = list1.stream().filter(item -> !list2Set.contains(item)).collect(Collectors.toList());
System.out.println("---得到差集 reduce1 (list1 - list2)---");
reduce1.parallelStream().forEach(System.out::println);
// 差集 (list2 - list1)
List<EamData> reduce2 = list2.stream().filter(item -> !list1Set.contains(item)).collect(Collectors.toList());
System.out.println("---得到差集 reduce2 (list2 - list1)---");
reduce2.parallelStream().forEach(System.out::println);
// 并集
List<EamData> listAll = list1.parallelStream().collect(Collectors.toList());
List<EamData> listAll2 = list2.parallelStream().collect(Collectors.toList());
listAll.addAll(listAll2);
System.out.println("---得到并集 listAll---");
listAll.parallelStream().forEach(System.out::println);
// 去重并集
list1Set.addAll(list2Set);
List<EamData> listDistinctAll = new ArrayList<>(list1Set);
System.out.println("---得到去重并集 listDistinctAll---");
listDistinctAll.parallelStream().forEach(System.out::println);
结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c4109f6c6b2926df1b959f4be8e1a0b1.png)
有ID情况下-和上面同理
//相同
List<EamData> result3 = list2.stream().filter(
(studentForPayResult) ->!list1Set.stream().map(EamData::getId).collect(Collectors.toList()).contains(studentForPayResult.getId())
).collect(Collectors.toList());
result3.parallelStream().forEach(System.out::println);
// 差集 (list2 - list1)
List<EamData> result4 = list2.stream().filter(
(eamData) ->!list1Set.stream().map(EamData::getId).collect(Collectors.toList()).contains(eamData.getId())
).collect(Collectors.toList());
result4.parallelStream().forEach(System.out::println);
结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c4e40cacc7f660eaeb941f5b52887da5.png)