Java – use comparisonchain for object equal()\u0026\u0026 Objects. Equal ()… What are the benefits of guava
I just started using Google's guava collection (comparisonchain and objects) In my POJO, I was waiting for the equals method, so I did this first:
return ComparisonChain.start() .compare(this.id,other.id) .result() == 0;
But I also realized that I could use this:
return Objects.equal(this.id,other.id);
Moreover, I can't see clearly when comparing chains, because you can easily add more conditions, such as:
return Objects.equal(this.name,other.name) && Objects.equal(this.number,other.number);
The only advantage is that I can see if you especially need an int return It has two additional method calls (start and result) and is more complex for noob
Are there any obvious benefits in the comparison chain?
(yes, I also overwrite the hash code with the appropriate objects. Hashcode()
Solution
Comparisonchain allows you to check whether an object is smaller or larger than another object by comparing multiple properties (such as sorting a grid by multiple columns)
Objects. Equal can only check for equality