简体   繁体   中英

Compare new elements in set with old elements in another set - Java

I have Set of Object which has existing values and getting a new Set values as an update. If the new Set does contain the old Object then I do nothing, if the new Set contains new Object then I want send create update and if the new Set doesn't contain an existing object then I want send a delete update.

Object has two fields :

  private PreferenceType preferenceType;
  private String preferenceValue;

Currently I am comparing the existing Set objects against new Set objects and if I don't find any existing object in new set then sending the delete update.

private void sendDeletePreferenceMessage(Set<AccountPreference> existingAccountPreferences, Set<AccountPreference> accountPreferencesFromRequest) {
    int counter = 0;
    for(AccountPreference accountPreference : existingAccountPreferences) {
      for(AccountPreference accountPreference1: accountPreferencesFromRequest) {
        if(accountPreference.getPreferenceType().equals(accountPreference1.getPreferenceType()) &&
                accountPreference.getPreferenceValue().equals(accountPreference1.getPreferenceValue()))
          counter++;
      }
      if(counter == 0) {
        accPrefDeleteSender.send(accountPreference);
      }
    }
  }

And also comparing the new set of objects against existing set of Objects to find the new updates that I want send as a create update

private void sendCreatePreferenceMessage(Set<AccountPreference> accountPreferencesFromRequest, Set<AccountPreference> existingAccountPreferences) {
    int counter = 0;
    for(AccountPreference accountPreference : accountPreferencesFromRequest) {
      for(AccountPreference accountPreference1: existingAccountPreferences) {
        if(accountPreference.getPreferenceType().equals(accountPreference1.getPreferenceType()) &&
                accountPreference.getPreferenceValue().equals(accountPreference1.getPreferenceValue()))
          counter++;
      }
      if(counter == 0) {
        accPrefCreateSender.send(accountPreference);
      }
    }
  }

This works perfectly but I believe this could be simplified in a better way. Any suggestion of doing this better!

You are using two AccountPreference Objects: one with id field, and another without id. I don't know if it's possible without using interfaces or abstract classes. Nevertheless, according to your comments it seems to me you're confused how to override Object.equals method, so I'll give you an example:

class MyObjectWithId {

    private long id;
    private String someField;

    public String getSomeField() {
        return someField;
    }


    @Override
    public boolean equals(Object other){
        if (this == other) return true;
        if (other == null || !other.getClass().isInstance(MyObject.class)) return false;
        MyObject myObject = (MyObject) other;
        return this.getSomeField().equals(myObject.getSomeField());
    }
}

class MyObject {

    private String someField;

    public String getSomeField() {
        return someField;
    }

    @Override
    public boolean equals(Object other){
        if (this == other) return true;
        if (other == null || !other.getClass().isInstance(MyObjectWithId.class)) return false;
        MyObjectWithId myObjectWithId = (MyObjectWithId) other;
        return this.getSomeField().equals(myObjectWithId.getSomeField());
    }
}

As you can see, you can ask if the two different types of Object are equal (in this case, there isn't need for overriding hashCode ).

Now you should be able to do the rest (regarding your original question about two Sets)

Set has a method boolean contains(Object o) .

You don't need two for loops, just iterate through one Set and check if another Set contains() the object!

You also need to override the boolean equals(Object o) in the POJO!

class MyObject {

private PreferenceType preferenceType;
private String preferenceValue;

@Override
public boolean equals(Object o){
    if (this == o) 
        return true;

    if (o == null || o.getClass()o.getClass() != getClass()) 
      return false;

    if(this.getPreferenceType().equals(o.getPreferenceType()) && this.getPreferenceValue().equals(o.getPreferenceValue()))
        return true;

    return false;

    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM