CodeGym /Courses /Java Multithreading /Practice with equals and hashCode

Practice with equals and hashCode

Java Multithreading
Level 1 , Lesson 5
Available

"Hi, Amigo!"

"Hi, Diego!"

"Good old Diego has prepared several tasks for you:"

24
Task
Java Multithreading, level 1, lesson 5
Locked
Equals and HashCode
In the Solution class, fix the equals/hashCode methods according to the implementation rules for these methods (use your favorite search engine to check the details). Both the first and last strings should be involved in the equals method's comparison and the calculation of the hashcode. The main me
6
Task
Java Multithreading, level 1, lesson 5
Locked
Fix the bug. Comparing objects
Solution objects are not being compared correctly. Find the bug and fix it. The main method is not tested.
6
Task
Java Multithreading, level 1, lesson 5
Locked
Bug in equals/hashCode
Correct the implementation errors in the Solution's classes equals and hashCode methods.
Comments (20)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Daniel Ketcheson Level 28, Canada
18 December 2023
One Line At A Time
Hoist Level 2, San Diego, United States
4 April 2023
SEE Ivan TIP below. >> You just got 10+ hours back to see daylight ...
FrogTea Level 23
27 February 2023
Code -> Generate -> Generate equals() and hashCode(). Click the suitable fields and finito 🤓
Ibrahim Level 41, Sheffield, United Kingdom
21 March 2022
Here is a good explanation of the equals and hashcode methods for help on the tasks above: https://codegym.cc/groups/posts/264-equals-and-hashcode-methods-best-practices
Naughtless Level 41, Olympus Mons, Mars
30 September 2021
Seems like you can use Objects.equals() or Objects.compare(), but that sort of beats the point of the lesson I suppose.
Henrique Level 41, São Paulo, Brazil
16 July 2020
I passed easily every task just using the automatic "Generate hashCode() and equals()..." of the IDE. But I didn't really understand these codes, and so far Code Gym hasn't explained what we need to complete these tasks. Will these methods be better explained and exemplified in this course, or will I have to research them somewhere else? The codes that passed the verification are much more sophisticated and complex than the ones of the examples in this lesson. Is it really expected from us, learners, to solve these tasks by our own, without copying/pasting? Or the purpose is indeed to make extra research, copy/paste, and then understand it only in the future?
Pablo Saquilmer Level 32, Guatemala City, Guatemala
28 October 2020
do they explain later in the course?
Andrei Level 41
22 March 2021
I don't think they explain later in the course because, as they (CodeGym people) have mentioned multiple times during the course, we have to do research on our own.
Jurij Thmsn Level 29, Flensburg, Germany
11 June 2021
So, after all - is it worth doing further research on it? I read two tutorials on it - but IntelliJ does it for me anyway ...
Hoist Level 2, San Diego, United States
4 April 2023
See Ivan TIPs below ...
Justin Johnson Level 31, Clearwater, United States
18 March 2020
Effective Java has a good section on this topic.
yz Level 37, Jakarta, Indonesia
14 December 2019
there is Objects class
Agent Smith Level 38
23 September 2020

Objects.compare(o1, o2);
Yes, this solves the null problem.
Ian De Bie Level 28, Austin, United States
7 November 2020
this works too

return Objects.equals(last, solution.last);
Senned Level 41, Azov, Russia
30 September 2019
Alt+Insert in IntelIDEA
Jason Level 26, Rancho Cucamonga, United States
12 December 2019
YES! saved so much work. between this tip and Ivan's things went great.
Henrique Level 41, São Paulo, Brazil
16 July 2020
What is "Alt+Insert" for?
Ivan Duka Level 22, Winnipeg, Canada
30 April 2019
Big hint for all three tasks: do not try to fix their code, remove their equals and hash methods and write them from scratch and you will pass the test. The only thing to watch out is to check explicitly if the object in comparison is an instance of Solution (as opposed to checking for generic getClass() != secondObject.getClass())
Luyi Level 31, Kosice, Slovakia
8 October 2019
many thanks for hint!
Johannes Level 27, Centurion, Pretoria, South-Africa
24 April 2020
Wish I saw Ivan's tip BEFORE I struggled myself into oblivion, lol!