Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java : Hashcode of a Class A containing a Class B which contains A

Tags:

java

I have a problem while implementing the hashcode function in my classes. As explained in the title, I have two classes:

  • The class A represents a school test, and so, it contains several attribute (i.e subject, mark, and an instance of the class representing the student who took the test).

  • The class B is as you can guess, the one representing the student. It has some attributes (i.e name, address, age, etc) AND an ArrayList containing multiple A class instance (the student can take many tests).

There comes my problem, I am asked to implement the hashcode functions in both classes. The hashcode I use is the one i've been taught, which is adding the hashcode of every attributes, multiplied by an integer depending of it's type(i.e multiply by 31 for Strings, 17 for int, 13 for Objects, etc).

But if i call the hashcode of the A class, then the hashcode of the B class is called, and then it calls back the hashcode of A class.

How do I get rid of this loop? I thought of getting rid of the hashcode call of the student class (A) hashcode in the test class (B). But are there any other option? It's my first attempt to get help, I hope I didnt make it to hard to understand.

like image 730
Wildrat Avatar asked Mar 14 '19 08:03

Wildrat


People also ask

What is the hashCode () and equals () used for?

Java hashCode() An object hash code value can change in multiple executions of the same application. If two objects are equal according to equals() method, then their hash code must be same. If two objects are unequal according to equals() method, their hash code are not required to be different.

What is the use of hashCode() method?

The hashCode() method is used to generate the hash values of objects. Using these hash values, these objects are stored in Java collections such as HashMap, HashSet and HashTable.

Can hashCode of two objects be same in Java?

1) If two objects are equal (i.e. the equals() method returns true), they must have the same hashcode. 2) If the hashCode() method is called multiple times on the same object, it must return the same result every time. 3) Two different objects can have the same hash code.

Is hashCode memory address in Java?

No, it is just an identifer. It's a unique value for that object; it need not represent the memory address.


1 Answers

One way to avoid your problem is to change the abstraction. The class that represents a school test ("class A") doesn't have to hold a reference of the student instance ("class B"). Instead, it can hold some identifier of that student (some unique property of the student).

Another way: I believe it makes sense to say that a student is not defined by the tests that they take. This means that two student instances that are equal in all properties except of the list of tests are referring to the same student. This implies that the list of tests should not participate in the hashCode() and equals() methods.

like image 87
Eran Avatar answered Oct 22 '22 21:10

Eran