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 - 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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
Abating setter and getter code bloat4m 52s
-
(Locked)
Adding but reducing ToString, Equals, and HashCode methods4m 22s
-
(Locked)
Pairing down constructor bloat3m 57s
-
(Locked)
One annotation to rule them all5m 20s
-
(Locked)
Playing nicely with Spring @Autowired4m 28s
-
(Locked)
JEP 359 record versus @Data and @Value5m 6s
-
(Locked)
Challenge: Reduce boilerplate code in POJOs3m 29s
-
(Locked)
Solution: Reduce boilerplate code in POJOs5m 23s
-
-
-
-