Java两个列表根据属性去重,java两个List对比根据某几个属性相同进行去重

在实际应用中总会有那个几个蛋疼的场景,其中之一就是要根据某几个属性相同来定义为重复,进行List去重,不想重复循环比较

Listlist1 = new ArrayList<>();

Listlist2 = new ArrayList<>();

Listlist3 = new ArrayList<>();

Map map1 = new HashMap();

map1.put("sku","1" );

map1.put("businessLine","1" );

map1.put("warehouseCode","1" );

Map map2 = new HashMap();

map2.put("sku","2" );

map2.put("businessLine","2" );

map2.put("warehouseCode","2" );

Map map3 = new HashMap();

map3.put("sku","3" );

map3.put("businessLine","3" );

map3.put("warehouseCode","3" );

Map map4 = new HashMap();

map4.put("sku","2" );

map4.put("businessLine","2" );

map4.put("warehouseCode","2" );

map4.put("code","2" );

Map map5 = new HashMap();

map5.put("sku","3" );

map5.put("businessLine","3" );

map5.put("warehouseCode","3" );

map5.put("code","2" );

list1.add(map1);

list1.add(map2);

list1.add(map3);

list2.add(map4);

list2.add(map5);

# 循环要排除的List 取出要

list2.forEach(x->{

Map map = new HashMap();

map.put("sku", x.get("sku"));

map.put("businessLine", x.get("businessLine"));

map.put("warehouseCode", x.get("warehouseCode"));

list3.add(map);

});

System.out.println(list3.toString());

list1.removeAll(list3);

// 使用filter

for (Map map : list2) {

list1 = list1.stream().filter(x->!(map.get("sku").equals(x.get("sku"))&&map.get("businessLine").equals(x.get("businessLine"))&&map.get("warehouseCode").equals(x.get("warehouseCode")))).collect(Collectors.toList());

}

System.out.println(list1.toString());

# 使用removerIf方法

for (Map map : list2) {

list1.removeIf(x->(map.get("sku").equals(x.get("sku"))&&map.get("businessLine").equals(x.get("businessLine"))&&map.get("warehouseCode").equals(x.get("warehouseCode"))));

}

# 返回结果

[{businessLine=1, sku=1, warehouseCode=1}]

可以使用Java中的Stream API来实现这个功能。具体实现方法如下: 1. 使用Stream的map方法将集合中的对象转化成属性值的集合; 2. 使用Stream的distinct方法去重; 3. 使用Stream的count方法统计去重后的元素个数,如果为1则说明两个集合中的对象某两个属性值全部相等,否则不相等。 示例代码如下: ```java public static boolean isPropertyEqual(List<?> list1, List<?> list2, String propertyName1, String propertyName2) { // 将集合中的对象转化成属性值的集合 List<Object> propertyList1 = list1.stream().map(obj -> { try { Field field1 = obj.getClass().getDeclaredField(propertyName1); field1.setAccessible(true); Field field2 = obj.getClass().getDeclaredField(propertyName2); field2.setAccessible(true); return Arrays.asList(field1.get(obj), field2.get(obj)); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); return null; } }).collect(Collectors.toList()); // 将集合中的对象转化成属性值的集合 List<Object> propertyList2 = list2.stream().map(obj -> { try { Field field1 = obj.getClass().getDeclaredField(propertyName1); field1.setAccessible(true); Field field2 = obj.getClass().getDeclaredField(propertyName2); field2.setAccessible(true); return Arrays.asList(field1.get(obj), field2.get(obj)); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); return null; } }).collect(Collectors.toList()); // 去重 propertyList1 = propertyList1.stream().distinct().collect(Collectors.toList()); propertyList2 = propertyList2.stream().distinct().collect(Collectors.toList()); // 统计去重后的元素个数,如果为1则说明两个集合中的对象某两个属性值全部相等,否则不相等 return propertyList1.size() == 1 && propertyList2.size() == 1 && propertyList1.get(0).equals(propertyList2.get(0)); } ``` 其中,list1和list2分别表示两个集合,propertyName1和propertyName2分别表示要比较两个属性名。返回值为一个布尔值,表示两个集合中的对象某两个属性值是否全部相等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值