1 00:00:00,760 --> 00:00:04,780 A hash code is only a semi unique identifier for an object. 2 00:00:04,780 --> 00:00:09,430 We've tried our best to make the hash codes unique, but it is still possible for 3 00:00:09,430 --> 00:00:12,340 two different objects to have the same hash code. 4 00:00:12,340 --> 00:00:16,340 Much like my name is Jeremy, but there are also other people named Jeremy. 5 00:00:16,340 --> 00:00:18,200 In fact, it's a rather common name. 6 00:00:18,200 --> 00:00:20,390 If someone called out my name in a large crowd, 7 00:00:20,390 --> 00:00:23,920 there'd be a good chance that more than one person would respond. 8 00:00:23,920 --> 00:00:27,590 How could they know which person is the Jeremy they're looking for? 9 00:00:27,590 --> 00:00:30,670 Well, they'd have to rely on more than just our name. 10 00:00:30,670 --> 00:00:34,070 They'd probably need to get a look at each of us in order to say 11 00:00:34,070 --> 00:00:35,650 which person they wanted. 12 00:00:35,650 --> 00:00:38,780 So whenever we override the GetHashCode method 13 00:00:38,780 --> 00:00:41,370 we should also override the Equals method. 14 00:00:41,370 --> 00:00:44,550 The Equals method is called to determine if two objects should be 15 00:00:44,550 --> 00:00:46,230 considered equivalent. 16 00:00:46,230 --> 00:00:48,940 If two objects map to the same hash code, 17 00:00:48,940 --> 00:00:52,220 the only way to tell them apart is to check if they're equal. 18 00:00:52,220 --> 00:00:54,750 Let's override the Equals method here. 19 00:00:54,750 --> 00:01:00,927 So say public override returns a bool and 20 00:01:00,927 --> 00:01:05,655 it just takes a type of object. 21 00:01:07,558 --> 00:01:11,659 The first thing we need to do is to make sure that this object passed in is of 22 00:01:11,659 --> 00:01:13,370 type student. 23 00:01:13,370 --> 00:01:19,292 So we'll attempt to cast it to a student using the as operator so 24 00:01:19,292 --> 00:01:23,554 say Student call that equals obj as Student. 25 00:01:25,090 --> 00:01:30,960 Now if obj isn't a student, then this as expression will return null. 26 00:01:32,120 --> 00:01:33,450 So we can check for that. 27 00:01:33,450 --> 00:01:36,017 Say if that equals null. 28 00:01:39,133 --> 00:01:41,650 Then will return false. 29 00:01:44,971 --> 00:01:47,990 If it isn't null, then it must have been a Student. 30 00:01:49,070 --> 00:01:53,830 So we can now check to see if each of the properties is the same. 31 00:01:53,830 --> 00:02:01,296 So say, return this.Name == that.Name. 32 00:02:01,296 --> 00:02:05,811 And, this.GradeLevel 33 00:02:05,811 --> 00:02:10,562 == that.GradeLevel. 34 00:02:10,562 --> 00:02:15,670 Now the Equals method will return true if all of the properties are the same. 35 00:02:15,670 --> 00:02:19,811 Now that we've overridden the GetHashCode and Equals methods, let's compile and 36 00:02:19,811 --> 00:02:22,447 run this to see if we've achieve the desired effect. 37 00:02:27,362 --> 00:02:33,680 So now both hashCodes are the same, and there's only one Joe in the Student set. 38 00:02:33,680 --> 00:02:37,410 HashSets organize objects internally by their hashCode. 39 00:02:37,410 --> 00:02:41,640 Once the hashCode has been computed, the set can quickly determine whether or 40 00:02:41,640 --> 00:02:43,600 not the object with that code is in the set. 41 00:02:44,660 --> 00:02:48,270 Adding an item to a hash set isn't necessarily faster than 42 00:02:48,270 --> 00:02:49,620 adding it to a list. 43 00:02:49,620 --> 00:02:53,300 However, the hash set is substantially faster when it comes to 44 00:02:53,300 --> 00:02:55,130 finding the item again. 45 00:02:55,130 --> 00:03:00,000 Sets are very fast at determining if an item is already in the collection. 46 00:03:00,000 --> 00:03:01,885 We can do this by calling the contains method. 47 00:03:07,448 --> 00:03:11,680 So here we could say something like, 48 00:03:11,680 --> 00:03:15,372 if students.Contains, Joe. 49 00:03:15,372 --> 00:03:18,470 Then, do something. 50 00:03:18,470 --> 00:03:21,810 The Contains method is defined in the I Collection interface. 51 00:03:21,810 --> 00:03:24,740 So every collection implements this method. 52 00:03:24,740 --> 00:03:29,040 Calling Contains on a list requires the list to look for the item by looking at 53 00:03:29,040 --> 00:03:32,470 each item one by one from the beginning of the list to the end. 54 00:03:32,470 --> 00:03:35,000 Calling Contains on a hash set on the other hand 55 00:03:35,000 --> 00:03:37,250 just requires computing the hashCode of the object. 56 00:03:38,310 --> 00:03:41,890 I've included more information about how this works in the teacher's notes. 57 00:03:41,890 --> 00:03:45,820 Likewise calling Remove on a hash set is also extremely fast. 58 00:03:45,820 --> 00:03:49,350 Remove requires first find the item and then removing it. 59 00:03:49,350 --> 00:03:52,760 We just learned that hash sets can quickly find items and 60 00:03:52,760 --> 00:03:56,730 since hash sets are unordered, there's no shifting of items in the collection 61 00:03:56,730 --> 00:03:58,620 after the item has been removed. 62 00:03:58,620 --> 00:04:02,400 This makes hash sets remove method substantially faster than lists.