In this solution, we're going to finish part four.
The Geter receives a string and from the string in each the return, the key that the caller is interested
in, in the article, I showed you four different ways to accomplish this task.
I'm going to start off with the most elegant way anyway's.
We can run through every key and a hash map as a key set this scoreboard key set and we're going to
run the key, sat through a stream pipeline stream and recommit.
A stream is a sequence of elements that we can run through a pipeline of functions.
And the first function, the first operation is going to be filter filter in effect, filters every
element that matches a predicates.
The lambda expression receives every key as a parameter and the arrow key points to a condition that
determines the filtering process.
And we're going to filter out the key whose house name matches the one being passed in.
At this point, there should only be one key, so we'll say find first and return the key that it finds.
And if it can't find anything, we'll return No.
So that's one way you can do it.
If you're satisfied with the way you can feel free to skip to this timestamp.
Otherwise, I can show you the three other approaches.
OK, I'm going to use the same pipeline, but instead of running through each key in the hash map,
I'll run through every entry.
And now we're getting errors because the lambda expression receives each entry.
And from the entry, we'll get the key.
And now we're filtering the entry that matches this predicate, OK, and sends the stream is now a sequence
of entries, find first returns, the first entry that it finds, but we need to return the key.
So at the end, we need to say get key.
OK, and now the main takeaway is that a hash map has entries, each entry contains a key and a value,
and we can choose to run through every key or every entry.
Can I will change up the pipeline, put this back as a Keystart?
You can remove the jet ski at the end because now the stream is already a sequence of keys and we can
use a different terminal operation collects and the collector we're going to choose from the list of
collectors is to list.
We expect to list only have one filtered key, so we're going to grab the element at index zero.
That's another way to do things.
All right, another final thing we can do is run through every entry instead.
And using the same pipeline, the list should now contain only one elements here, we're indexing that
entry and we're going to grab that entry's key.
That's what we need to return to.
The main takeaway is that there are so many ways to accomplish the same thing.
We could have even used the loop to run through every entry in the entry set or every key in the key
set.
But obviously a pipeline is much cleaner and more concise.
All right, I'm going to go back to using the first pipeline find first.
And run through each key in the key set, because I think it's the most elegant approach.
All right, let's test it out, let me clean up here.
And get the team Gryffindor.
And beautiful.
Our final task was to create a static variable called game count static int game count.
And game count is going to count the number of games played.
The reason this variable is static is because it doesn't make sense for a game object to have a field
called game count.
The game count is the same across all objects.
The number of games played relates to the class, not objects of the class.
And we want to be able to access this variable directly from the class type anyways every time a new
game is played.
In other words, every time a new game object is created, add one to the count.
OK, now I'm going to create three objects of the game class.
Change the variable names.
And print the game counts.
OK, since it's in a different folder, I need the visibility to be explicitly public.
OK, and we got three cool.
But like fields, this static variable should be private because we don't want the caller to have access
to it.
This is a security risk because the caller may update it by mistake.
So we'll need to make this private's.
And create a GETER, which I can use autocomplete.
And IT autocomplete, a static getter cool, the getter returns a copy of the variable, its value.
And now we can call the static method from meina.
And everything works perfectly.
Our variable effectively keeps track of how many games are played.
We're actually not going to end up using game count as part of the final simulation.
But I just wanted you to get some more practice with static variables.