From the course: Learning Lombok: Streamlined Java Programming

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Adding but reducing ToString, Equals, and HashCode methods

Adding but reducing ToString, Equals, and HashCode methods - Spring Tutorial

From the course: Learning Lombok: Streamlined Java Programming

Adding but reducing ToString, Equals, and HashCode methods

- [Instructor] We removed the setter and getter boilerplate, but what if we could also properly generate the toString, the equals, and the hashCode method with annotations. Well, I think you know where I'm going with this. I have project 01_02 begin LombokDemo opened, with the Employee class loaded in IntelliJ. Now, it might not seem so obvious why these annotations are important, but consider that the default toString method is not very useful when it is output. Josh Bloch points out in his book, "Effective Java", that the object contract says, "It is recommended that all subclasses override the toString Method, so the code is easier to read." Similarly, we need to override the hashCode method, if the equals method is to be overridden. In essence, we need to do this to avoid violations of the general contract of the Object.hashcode, and ensure that the hash-based collections function properly with your code, like that of…

Contents