Java Two Sister comparisons are based on the same attributes.

In actual application, there will always be some painful egg scenes. One of them must be defined as duplicates according to the same attributes.

List<Map> list1 = new ArrayList<>();
List<Map> list2 = new ArrayList<>();
List<Map> list3 = 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);
  # 

  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);
// Используйте фильтр  

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());
# Используйте метод снятия снятия

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}]

Leave a Comment