1 00:00:00,830 --> 00:00:02,690 Let's circle back to the Quidditch project. 2 00:00:03,380 --> 00:00:08,420 I ended the last Quidditch lesson by saying that for Hashmark to retrieve the key, it's not enough 3 00:00:08,420 --> 00:00:10,030 for an object to be equal to it. 4 00:00:10,280 --> 00:00:12,320 It must also share the same hash code. 5 00:00:16,210 --> 00:00:19,810 Hash map uses the hash code to find which bucket the key is in. 6 00:00:21,920 --> 00:00:26,780 And after it finds the bucket it can use equals to compare your object against the key. 7 00:00:29,160 --> 00:00:34,420 So when you add an equals method, you must always add a hash code method that assigns equal object 8 00:00:34,420 --> 00:00:35,490 to the same hash code. 9 00:00:35,910 --> 00:00:38,220 If you don't do that, you're going to get bugs. 10 00:00:39,870 --> 00:00:45,570 So here in Team Not Java, since we have an equals method, we must also add a hash code method, public 11 00:00:45,570 --> 00:00:46,650 and tache code. 12 00:00:50,090 --> 00:00:54,830 And here we can use the hash method to assign an object to hash code based on its fields'. 13 00:00:57,910 --> 00:00:59,050 In this case, house. 14 00:01:01,930 --> 00:01:03,070 Keeper seeker. 15 00:01:05,379 --> 00:01:10,720 And from experience, the hash method doesn't work with a race, so pass the string representation of 16 00:01:10,720 --> 00:01:15,790 the array, which is the same thing because equal objects are going to end up having the same Chaser's 17 00:01:15,790 --> 00:01:16,450 string. 18 00:01:16,960 --> 00:01:20,890 Ultimately, this hash method is going to assign equal object to the same hash code. 19 00:01:21,400 --> 00:01:23,350 And now I'm confident that our code should work. 20 00:01:47,340 --> 00:01:53,100 And it does work get score returns, the score for Gryffindor, the parameter we're passing in is equal 21 00:01:53,100 --> 00:01:56,730 to the key that we're interested in, which means they have the same hash code. 22 00:01:57,790 --> 00:01:59,490 We can test set score as well. 23 00:02:02,520 --> 00:02:08,490 And it works at Update's Gryffindor score to 10, the hash map was able to find a key that matches the 24 00:02:08,490 --> 00:02:11,580 hash code and a quality of the one we're passing in. 25 00:02:11,790 --> 00:02:15,000 So it knows that we're trying to update this particular key. 26 00:02:15,570 --> 00:02:17,010 So I really want to stress this. 27 00:02:17,250 --> 00:02:21,750 When you add an equals method, you must always, always add a hash code method. 28 00:02:22,140 --> 00:02:25,380 Otherwise you're going to get bugs from hash based collections.