I've been reading about changing hashCode() function in java.
There's one thing I don't really understand:
If I have a class Person, and store a HashMap in a class like:
Map<String, Person> map = new HashMap<>(); does the defined hashCode() function do anything?
It's just that from what I understand, it would be using the String's hashCode, instead of Person's HashCode() correct?
So if I have this class
public class Person {
private int age;
private String name;
public boolean equals {...}
public int hashCode() {
return Objects.hash(age, name);
}
}
And on main I store a Map of these objects like this:
public class Main {
public static void main (String [] arg) {
Map <String, Person> pers = new HashMap<>();
Person p = new Person("Peter", 21); //Imagine I have this constructor
pers.put(p.getName(), p);
}
}
Would this use the hashCode() function defined in the Person class?