在Java中基于属性查找重复对象

问题描述 投票:0回答:3

这是我的对象结构。

List<Customer> customerSelection = new ArrayList<Customer>();
    Customer c1 = new Customer();
    Customer c2 = new Customer();

    c1.setName("Syed");
    c2.setName("Syed");
    Map<String,String> locationList1 = new HashMap<String,String>();
    Map<String,String> locationList2 = new HashMap<String,String>();
    locationList1.put("DXB", "Dubai");
    locationList1.put("AUH", "Abudhabi");

    locationList2.put("DXB", "Dubai");
    locationList2.put("BAH", "Bahrain");

    c1.setLocationList(locationList1);
    c2.setLocationList(locationList2);

    customerSelection.add(c1);
    customerSelection.add(c2);

在这里,我想验证客户是否有首选的重复位置,我应该抛出一条错误消息。关于优化解决方案的想法?

这里Syed将迪拜作为位置列表中的位置无效。

java
3个回答
1
投票

您可以在添加时检查位置是否重叠:

safeAdd(customerSelection, c2);

使用Collections.disjoint,如果两个指定的集合没有共同的元素,则返回true。

private static void safeAdd(List<Customer> customerSelection, Customer newCustomer) {
    if (customerSelection
            .stream()
            // look at all the users with matching name
            .filter(customer -> customer.name.equals(newCustomer.name))
            // ensure all of them have no location overlap with the new customer
            .allMatch(customer -> Collections.disjoint(customer.locationList.keySet(), newCustomer.locationList.keySet()))) {
        customerSelection.add(newCustomer);
    } else {
        throw new RuntimeException("Cannot add customer, customer with this name exists with this location");
    }
}

或Java 7:

private static void safeAdd(List<Customer> customerSelection, Customer newCustomer) {
    for (Customer customer : customerSelection) {
        if (customer.name.equals(newCustomer.name) &&
                !Collections.disjoint(customer.locationList.keySet(), newCustomer.locationList.keySet())) {
            throw new RuntimeException("Cannot add customer, customer with this name exists with this location");
        }
    }
    customerSelection.add(newCustomer);
}

0
投票
You can iterate through list and check whether HashMap has same key present in other Customer's locationList.

for(int i=0; i<customerSelection.size()-1;i++){
   for(int j=0;j<customerSelection.size()-1;j++){
     if(j!=i){
   HashMap map   =    customerSelection.get(i).getLocationList();
   for(Map.Entry<> entry: map.entrySet()){
      if(customerSelection.get(j).getLocationList().containsKey(entry.getValue())) {
        THROW ERROR               
             }
      }

      }
   }

}

0
投票

请看这里

    public static void main(String[] args) {
        List<Customer> customerSelection = new NonDuplicateLocationsList();
        Customer c1 = new Customer();
        Customer c2 = new Customer();

        c1.setName("Syed");
        c2.setName("Syed");
        Map<String,String> locationList1 = new HashMap<String,String>();
        Map<String,String> locationList2 = new HashMap<String,String>();
        locationList1.put("DXB", "Dubai");
        locationList1.put("AUH", "Abudhabi");

        locationList2.put("DXB", "Dubai");
        locationList2.put("BAH", "Bahrain");

        c1.setLocations(locationList1);
        c2.setLocations(locationList2);

        customerSelection.add(c1);
        customerSelection.add(c2);
    }
}
class Customer {
    Map<String, String> locations;
    String name;

    public Map<String, String> getLocations() {
        return locations;
    }

    public void setLocations(Map<String, String> locations) {
        this.locations = locations;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

class NonDuplicateLocationsList extends ArrayList<Customer> {


    @Override
    public boolean add(final Customer customer) {


        for (Customer customer1 : this) {
            if (Maps.difference(customer1.getLocations(),customer.getLocations()).entriesInCommon().size() > 0) {
                throw new RuntimeException("Has Location duplicates");
            }

        }
        return super.add(customer);
    }
}

© www.soinside.com 2019 - 2024. All rights reserved.