1 00:00:00,180 --> 00:00:02,190 In this solution, we're going to finish part four. 2 00:00:05,190 --> 00:00:09,720 The Geter receives a string and from the string in each the return, the key that the caller is interested 3 00:00:09,720 --> 00:00:13,920 in, in the article, I showed you four different ways to accomplish this task. 4 00:00:14,160 --> 00:00:17,220 I'm going to start off with the most elegant way anyway's. 5 00:00:17,220 --> 00:00:22,620 We can run through every key and a hash map as a key set this scoreboard key set and we're going to 6 00:00:22,620 --> 00:00:27,030 run the key, sat through a stream pipeline stream and recommit. 7 00:00:27,030 --> 00:00:31,110 A stream is a sequence of elements that we can run through a pipeline of functions. 8 00:00:31,620 --> 00:00:36,900 And the first function, the first operation is going to be filter filter in effect, filters every 9 00:00:36,900 --> 00:00:38,640 element that matches a predicates. 10 00:00:39,060 --> 00:00:44,370 The lambda expression receives every key as a parameter and the arrow key points to a condition that 11 00:00:44,370 --> 00:00:46,410 determines the filtering process. 12 00:00:47,520 --> 00:00:51,720 And we're going to filter out the key whose house name matches the one being passed in. 13 00:01:00,020 --> 00:01:05,810 At this point, there should only be one key, so we'll say find first and return the key that it finds. 14 00:01:11,070 --> 00:01:13,170 And if it can't find anything, we'll return No. 15 00:01:18,530 --> 00:01:20,320 So that's one way you can do it. 16 00:01:21,390 --> 00:01:25,080 If you're satisfied with the way you can feel free to skip to this timestamp. 17 00:01:27,840 --> 00:01:30,990 Otherwise, I can show you the three other approaches. 18 00:01:31,500 --> 00:01:36,270 OK, I'm going to use the same pipeline, but instead of running through each key in the hash map, 19 00:01:36,270 --> 00:01:37,710 I'll run through every entry. 20 00:01:38,190 --> 00:01:42,300 And now we're getting errors because the lambda expression receives each entry. 21 00:01:43,670 --> 00:01:45,710 And from the entry, we'll get the key. 22 00:01:52,370 --> 00:01:58,370 And now we're filtering the entry that matches this predicate, OK, and sends the stream is now a sequence 23 00:01:58,370 --> 00:02:04,260 of entries, find first returns, the first entry that it finds, but we need to return the key. 24 00:02:04,280 --> 00:02:06,530 So at the end, we need to say get key. 25 00:02:10,610 --> 00:02:16,940 OK, and now the main takeaway is that a hash map has entries, each entry contains a key and a value, 26 00:02:17,270 --> 00:02:20,480 and we can choose to run through every key or every entry. 27 00:02:22,210 --> 00:02:25,180 Can I will change up the pipeline, put this back as a Keystart? 28 00:02:29,630 --> 00:02:35,270 You can remove the jet ski at the end because now the stream is already a sequence of keys and we can 29 00:02:35,270 --> 00:02:40,640 use a different terminal operation collects and the collector we're going to choose from the list of 30 00:02:40,640 --> 00:02:42,380 collectors is to list. 31 00:02:45,770 --> 00:02:51,230 We expect to list only have one filtered key, so we're going to grab the element at index zero. 32 00:02:58,530 --> 00:03:00,010 That's another way to do things. 33 00:03:00,430 --> 00:03:04,060 All right, another final thing we can do is run through every entry instead. 34 00:03:09,880 --> 00:03:15,730 And using the same pipeline, the list should now contain only one elements here, we're indexing that 35 00:03:15,730 --> 00:03:18,360 entry and we're going to grab that entry's key. 36 00:03:18,370 --> 00:03:19,840 That's what we need to return to. 37 00:03:20,410 --> 00:03:24,420 The main takeaway is that there are so many ways to accomplish the same thing. 38 00:03:24,760 --> 00:03:30,160 We could have even used the loop to run through every entry in the entry set or every key in the key 39 00:03:30,160 --> 00:03:30,640 set. 40 00:03:31,000 --> 00:03:34,150 But obviously a pipeline is much cleaner and more concise. 41 00:03:35,340 --> 00:03:38,490 All right, I'm going to go back to using the first pipeline find first. 42 00:03:47,210 --> 00:03:51,410 And run through each key in the key set, because I think it's the most elegant approach. 43 00:03:56,100 --> 00:03:58,010 All right, let's test it out, let me clean up here. 44 00:04:00,660 --> 00:04:02,250 And get the team Gryffindor. 45 00:04:23,750 --> 00:04:24,770 And beautiful. 46 00:04:32,210 --> 00:04:37,640 Our final task was to create a static variable called game count static int game count. 47 00:04:40,860 --> 00:04:43,540 And game count is going to count the number of games played. 48 00:04:43,770 --> 00:04:48,930 The reason this variable is static is because it doesn't make sense for a game object to have a field 49 00:04:48,930 --> 00:04:50,220 called game count. 50 00:04:50,410 --> 00:04:53,580 The game count is the same across all objects. 51 00:04:53,970 --> 00:04:58,830 The number of games played relates to the class, not objects of the class. 52 00:04:58,830 --> 00:05:04,260 And we want to be able to access this variable directly from the class type anyways every time a new 53 00:05:04,260 --> 00:05:05,030 game is played. 54 00:05:05,040 --> 00:05:09,630 In other words, every time a new game object is created, add one to the count. 55 00:05:11,130 --> 00:05:14,040 OK, now I'm going to create three objects of the game class. 56 00:05:17,020 --> 00:05:18,400 Change the variable names. 57 00:05:21,680 --> 00:05:23,120 And print the game counts. 58 00:05:34,120 --> 00:05:38,440 OK, since it's in a different folder, I need the visibility to be explicitly public. 59 00:05:51,940 --> 00:05:53,710 OK, and we got three cool. 60 00:05:56,400 --> 00:06:02,040 But like fields, this static variable should be private because we don't want the caller to have access 61 00:06:02,040 --> 00:06:02,490 to it. 62 00:06:02,700 --> 00:06:06,210 This is a security risk because the caller may update it by mistake. 63 00:06:07,730 --> 00:06:09,400 So we'll need to make this private's. 64 00:06:14,930 --> 00:06:17,810 And create a GETER, which I can use autocomplete. 65 00:06:26,750 --> 00:06:32,370 And IT autocomplete, a static getter cool, the getter returns a copy of the variable, its value. 66 00:06:32,720 --> 00:06:35,090 And now we can call the static method from meina. 67 00:06:49,440 --> 00:06:50,910 And everything works perfectly. 68 00:06:51,360 --> 00:06:54,640 Our variable effectively keeps track of how many games are played. 69 00:06:55,320 --> 00:06:59,220 We're actually not going to end up using game count as part of the final simulation. 70 00:06:59,220 --> 00:07:03,360 But I just wanted you to get some more practice with static variables.