Arno_Geismar
Arno_Geismar

Reputation: 2330

Equals method does not return true for same object

@IdClass=(value = TripleKey.class)
class Triple {
   String subject;
   String predicate;
   String object;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;

        Triple triple = (Triple) o;

        if (!subject.equals(triple.subject)) return false;
        return predicate.equals(triple.predicate);
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + subject.hashCode();
        result = 31 * result + predicate.hashCode();
        return result;
    }
}

my objects are:

{
    "subject": "http://www.someurl.com/thing/resources/<owner>#SenMLJSON",
    "predicate": "probes_in",
    "object":"http://sweet.jpl.nasa.gov/2.3/matrSediment.owl#clay"

}

and

{
        "subject": "http://www.someurl.com/thing/resources/<owner>#SenMLJSON",
        "predicate": "probes_in",
        "object":"http://sweet.jpl.nasa.gov/2.3/matrSediment.owl#sand"

    }

When I try the following I still have duplicates :

public static List<Triple> mergeTripleLists(List<Triple> oldList, List<Triple> newList) {
        Set<Triple> tripleSet = new HashSet<>(oldList);
        tripleSet.removeAll(newList);
        tripleSet.addAll(newList);
        return new ArrayList<>(tripleSet);

    }

Upvotes: 0

Views: 369

Answers (2)

Batman
Batman

Reputation: 11

The problem is the call to the equals method of the super class which uses object reference to test equality, so remove the line with

!super.equals(o)

You also need to remove the call to the hashCode method of the super class.

Upvotes: 1

xingbin
xingbin

Reputation: 28279

The problem is in:

if (!super.equals(o)) return false;

If should work after removing it.

Upvotes: 2

Related Questions