Quantcast
Channel: User Kunal - Stack Overflow
Viewing all articles
Browse latest Browse all 34

Can't Override Equals method on Java when calling contains

$
0
0

I have the following funtion that checks if a List of codigos contains a single codigos object:

if (!concorrente.getJcodigoses().contains(cod))         {            return "redirect:"+ referrer;        }

I read that i need to Override the equals method like so:

@Override    public boolean equals(Object object)    {        boolean isEqual= false;        if (object != null && object instanceof Jcodigos)        {            isEqual = (this.id == ((Jcodigos) object).id);        }        return isEqual;    }

I placed it in my Jcodigos.java class and i noticed that concorrente.getJcodigoses().contains(... never gets into my custom equals method...

Any advice?

Thanks

Answer:

I was missing the following method

@Overridepublic int hashCode() {    return this.id;}

Viewing all articles
Browse latest Browse all 34

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>