0

This is my object structure.

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);

Here I want to validate if a customer is having preferred duplicate location, I should throw an error message. Any ideas on optimized solution?

Here Syed is having dubai as as location in location list which is invalid.

14
  • Why do you need a Map instead of a List for the locations? Commented Feb 14, 2019 at 12:37
  • @ayrton, this is an existing object structure, so I didn't bother about it which is having location id and location name. Commented Feb 14, 2019 at 12:38
  • 3
    I am not fully understand what you are asking. You don't want to duplicate key and value or you don't want to duplicate keys, or you don't want to duplicate values? Commented Feb 14, 2019 at 12:39
  • @SaidAlır, the customerSelection list will have duplicate objects based on customer name. But customer should not give duplicate preferred locations. Commented Feb 14, 2019 at 12:40
  • 1
    So, requirement is, they can have different names and same location, or same name and different locations, but never same name and same location? Commented Feb 14, 2019 at 12:52

3 Answers 3

1

You can check the locations do not overlap at time of adding:

safeAdd(customerSelection, c2);

Using Collections.disjoint which returns true iff the two specified collections have no elements in common.

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");
    }
}

Or 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);
}
Sign up to request clarification or add additional context in comments.

8 Comments

::( I'm having JDK as 1.7, so I can't use 1.8.., If possible please let me know in 1.7
I'm sure you can work it out. Just use a for loop. Collections.disjoint is doing the hard work.
Thanks Weston. But how to call this method? while iteration of customerSelection? as I see you are passing safeAdd(customerSelection, c2); as hardcoded, because at the time of runtime, there can be multiple customers,right?
We'll you call safeadd rather than add. I don't really understand what you don't understand.
My point is why are we passing c2 in this method as hardcoded.. safeAdd(customerSelection, c2); we just have to iterate the list and validate it right? Please correct me if Im wrong
|
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               
             }
      }

      }
   }

}

2 Comments

for(Map.Entry<> entry: map.entrySet()){ .. This has compilation problem
Use Map.Entry<String,String>
0

please have a look here too

    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);
    }
}

4 Comments

So I create a new class extended from Araylist, override add method and check all the stuff there, so that no one can add duplicate items there
The locationlist is a map object, but not list object. Please check it again.
But, please check it with a unit testing. Thanks Hakob
@Syed I've updated the answer, tested for two cases, it works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.