Case 1: Overriding both equals (Object) and hashCode () method. You must override hashCode () in every class that overrides equals (). Failure to do so will result in a violation of the general contract for Object.hashCode (), which will prevent your class from functioning properly in conjunction with all hash-based collections, including
Try it Yourself » Definition and Usage The equals () method compares two strings, and returns true if the strings are equal, and false if not. Tip: Use the compareTo () method to compare two strings lexicographically. Syntax public boolean equals (Object anotherObject) Parameter Values Technical Details String Methods
Compare Enum Using the equals() Method in Java. Java equals() method compares two values and returns a boolean value, either true or false. We can use this method to compare enum values. Here, we used the Color enum to compare its values. The first value returns false, but it returns true for the second. See the example below.