Equals() and Hashcode() superscript

Asked

Viewed 162 times

1

I’m learning to use the equals() and hashCode(), and I was taught that Eclipse overwrites this method for us. But I’m trying to buy two attributes to tell if one object is the same as the other.

Better example: If a product has the same code, but its size is different, they should be considered different objects. I’m using HashMap for this implementation.

The Equals() and HashCode() are like this:

@Override
public int hashCode() {
    final int prime = 31;
    int result = super.hashCode();
    result = prime * result + ((super.getCodigo() == null) ? 0 : super.getCodigo().hashCode());
    result = prime * result + ((getTamanho() == null) ? 0 : getTamanho().hashCode());
    return result;
}


@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (!super.equals(obj))
        return false;
    if (getClass() != obj.getClass())
        return false;
    ProdutoComTamanho other = (ProdutoComTamanho) obj;
    if (super.getCodigo() == null) {
        if (other.getCodigo() != null)
            return false;
    } else
        return false;
    if (getTamanho() == null) {
        if (other.getTamanho() != null)
            return false;
    } else if (!getTamanho().equals(other.getTamanho()) && !super.getCodigo().equals(other.getCodigo()))
        return false;
    return true; 
}

When I test the application it doesn’t consider them as different objects, only when I change the product code.

2 answers

2


Eclipse just generates code for you, nothing more than this, whoever overrides it is the compiler. Read more in What is a programming language, IDE and compiler?.

What you call an attribute actually calls a field.

I don’t understand what you want with hashCode(). Almost no one talks about it, but it’s a bitch to have one hashCode() in Object. Almost always have this method in mutable classes, at least in the fields that are part of the code calculation hash, is an error. This code should be stable. If you use unstable code in a HashMap or something similar will have problems, you add it one way and fetch another, do not think.

If the conversion of obj for the guy ProdutoComTamanho fail your code will break, have to test if it worked.

There’s some weird stuff in the code, and without saying why you’re doing it, it could be right or wrong, we have no way of knowing. This inheritance for example looks bad, but there is case that may be appropriate.

The relational operator is wrong, just one of the two comparisons are different and it will already be different, so the correct is to use || and not &&. The operator of or requires only one to consider true and neither looks at the other if the first true duty, and as uses a denial in the first expression it is true if i code is different, it should already enter the if.

1

I did the tests with a class created with code, name and size. I automatically generated by eclipse and worked perfectly.

In your code, in some situations appears super.getCodigo(), I am deducing that the class Productthe size inherits from the class Product... take a look at the two classes, as it is an academic project the error may be in getCodigo of the super class.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.