From the course: Java Cheat Codes: Most Popular Functions

Unlock this course with a free trial

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

Solution: hashCode()

Solution: hashCode()

(upbeat music) - [Instructor] So let's go through one way of utilizing the hashCode method. We started off, as you heard in the challenge, with our private fields for our book information of title, author, and publication year. We have our constructor that initializes the objects with the provided book details. And then scrolling down, here we are calculating and returning a hashCode based on the title, which would be lowercase, and then our publication year. And then scrolling down even further, what I have here is comparing two answer objects for equality based on title, so it's going to be case-sensitive, and publication year. So this helps check for equality of the title, as well as I even go further on to return a string representation of the answer object in human readable form. This one's important because for some reason, it might be because of ISBN numbers or what have you, when you run this, you do receive the like, Java representation of your hashCode, so you need to put…

Contents