so continuing with our discussion about using maps so what we gonna do now is
used to remove method to remove a specific key from a map and to use this
probably as you thinking NOW the way to remove is logical is actually
two ways of doing so the first way we can do it is we can do something like, and what will do is
will leave the print out on the bottom there so we confirm that our changes
actually work so down here we can put…
then we can put… and that's gonna the list entry but we can also do something else we can remove
a key only if it's map to a certain value so for example we could do something
like this we can put…
and we put else…
so we run this code again, so we can see now from the list we got this
message Algol not remove key pair not found, but you can see that Lisp
has been successfully removed so just confirming again on line 34 we checking
to see whether Algol and the value of algorithmic languages was there and only
removed under those circumstances and obviously on line 21
when we defined it we didn't defined it like that and as a result it didn't
remove it for that reason, now the other thing to note is that the remove method does return true or
false that's good to know if you wanna check that to some was removed you can
actually put an if then and just check for true or false to indicate if the key is in the map
was removed and you see that there on line 34 because we used that function cuz we did
an if language was remove and pass the parameter and that returned true and
were able to use if then else just to confirm that works, I go back here and copy
the definition from line 21 and pasted into here on line 34 in the second parameter and if we run
this, this time you can see the message Algol was removed and we only got 3 items left both
Lisp and Algogl
was removed from the list of languages ok so let's just clean up a few thing because i want to talk about a few other
methods
so what I’m going to do first is to comment out this removal of list
then down here just before the for loop what we're going to do is print a message here and
use the dot replace method and what replaced does is it’s used to
replace the entry for a specified key if it's already mapped to a value so you see what I
mean by that we can do something along the lines of here will print out the results
languages…
…and we do something like…the first one but also
another entry as well just to confirm the results we get out of this and will do
something like…
and will try Scala…obviously because Scala doesn’t exist and will be replacing on the assumption that the key
exists
so we run this, so you can see what's happened, therein lies madness when
this first one replaced on line 43 when that code was executed it return the
previous entry which of course was therein lies madness which we configured on line 23 the
next entry their returned null which was its way of saying it did not already
exist on file and it's not added as you can see because it doesn't come up
with a list because it wasn't already in existence in the map so it doesn't get
added unlike put which does that and replaces it if it's already there and
just like remove we can also specify the existing value so the mappings only
update if the key was map to the Old value which we originally specify or what we can
specify to do this we provide the expected old value before the new one now what will do
will just do it wrong the first time so you can get an idea on what I mean so on this line here
instead of printing it out will change this code a little bit will put an if there…Lisp…
notice how its spawning 3 options theres key string value and string old value or we can just
do what we did in the past in this case we put something like the old value of this…and specify the new value like so…
else…ok and will just comment the Scala one for now, so we run that and you should find it doesn’t get replaced, so Lisp was not replace and Lisp
therein lies madness is still set to the original value which of course we configured on the
line 23 so as a recap on that particular function so what we're doing
is we're passing a third parameter, passing the key, passing the old value
and were passing the new value was and thats a way to checked if it really
was only updating if the value, this particularly already existed and just to
confirm that we can come back up here and copy and change this will not work, paste it in there like so and we run it, you can see that
Lisp was replaced and Lisp is now a functional programming language with imperative
features, so it has obviously
work through and updated which is what I wanted to do you might be wondering why
is that useful, one good use would be for example
if you're updating someone's name after they got married you can ensure that the
correct persons updated or changing the address so you just checking to make
sure that that exact entry at the exact right entry before you update it
so there is no chance of it accidentally overriding the wrong info, now for these examples
the methods defined by the map interface we’ve used the map that uses a string type for
the key and value now in fact both the key and the values can be any object we
could even use a map type as the values in another map in actual fact you could
add a map as a value to itself, its important to point out that unlike other
languages such as Python for example there's no requirement that the keys be
immutable and just as a recap immutable objects or things like strings and
integers who’s value cant change if it sounds strange when we saw assign a new string
value to a string variable, what we are doing we’re changing the value that the
variable holds we're not
changing the string so Algol for example that we used for one of the
keys earlier on line 21 can't be changed on the other hand an array list can change
by having new items added when a new value is assigned to existing entries, that said
the Java documentation does warn that great care must be exercised if
immutable objects are used as map keys so they give you that warning but the not
prohibited the reason for the warnings is that a map object can behave
unpredictably if the key objects change in such a way that equals comparisons
are affected
remember we talked about equal comparisons in the last video and actually earlier in this video, so it's worth being aware
of this warning but you probably have to do something fairly advanced before it affect you
so using classes as keys is in itself fairly advanced
now one thing that's not permitted is for a map to contain itself as a key and it would be a
bizarre program that attempted to do such a thing to demonstrate some more
probably more interesting uses of maps, I’m going to model a small part of the
original adventure game called colossal cave which allows a player to move around in various
locations by typing in compass directions so what we're going to do is
create a new project called an adventure and I'm going to create a new class
let's go ahead, so file, new Project, I’m just gonna do standard things that
that you've probably seen a million times now, we’re just going to create
the project from template, I'm gonna call this one Adventure and Finish it
And we gonna say, this window, which closed off the other code because we don’t need it for now, and were gonna go
ahead in the package
and we are gonna create a new class and call that Location, so lets start by defining some basic
information about this location and again this is the location in an adventure game based
on the original colossal cave adventure, which was one of the first adventure games that came
out years and years ago, and feel free to google that if you want to find out more
information about that so let's start, we,re gonna have a location ID first, so…
lets also have a description so…
…this is the description of the location…and were getting that error most likely because we haven’t initialize
but we're going to initialize those in a constructor shortly we also want a map
which is gonna be a map of exits so private…let’s add a constructor…
and the constructor is going to take just the two parameters so were going to take the
locationID and the description, we're not going to do the exists because we’re gonna
initialise that, and you notice that those two error have gone away now
because we initialized it and set our exits up…
we initials it to a new HashMap
and what we want to do is add a couple of method we want the ability to
add an exit to our location, we want a getter for location, and description and
a getter for our list and exits so let's go ahead and do that as well
so getters are easy enough so lets ad those first, select location,description and exits as getters, so we can get access to those from another class, and lets go up here
what we want to do is we want to add the ability to add an exit so to do that we can do
public…
and then put exits…
and our direction is going to be the key and the location is going to be our value that's a
very basic location, so its containing just to recap the location contains a
location ID and the description as well as a map that we defined in line 12 which is
used to store the value of exits when the location and we've added a method
to allow directions to be added to this map so that we can define the details from the
main program no one thing about this class is that we programming defensively
what I mean by that is anything that doesn't need to be exposed to the
outside isn’t exposed and our fields are mark final so they can't be changed once a
constructor has finished creating a location instance so one useful
technique that is in the getter for the exits map and with the get exits what I did
there, I want to explain to you why I'm doing this, so what I’m going to do there is
return
new…
of exits, I’m putting exits in the constructor so the reason we're doing that is that instead of just returning
the exits map and creating a new HashMap and passing out exits in the
constructor on line 32 now we’ve seen that use of the constructor
before with lists it does the same thing here, so in other words a new map created
that has all the makings of the exits map now the reasons that this would be useful
useful technique doing that here is that nothing outside of that class can change
exits, so the getter returns a copy of the exits so if the calling program wants to
add or remove mappings from it and the exits mapping field won't be affected
that's really important there so although we won’t be using the location class as a key in the map, we use that later to
show some ways to make your classes immutable and returning immutable object to getter in this
way is one of the techniques here to make sure that works so let's go back to our
main method and write some code to add some locations, so what I’m going to do is start
by bringing a scanner in their…scanner…and what I’m going to do is copy
and paste some of these locations to save time and I have six locations to start
with, these are locations in the game, and I also need to define the map here, so its private…
static…
HashMap…and obviously from lines 13 to 18 we are
adding new locations you can see we're adding a number, we’re putting the original
key there which is the integer 012345 which gets converted to an
integer object by auto boxing and you can see that we're creating a new
location object using the same technique we are passing the number which is going to be
sort of like a locationID then the description of what the particular location is all
about
so lets now add some code that allows us to type in a number and prints the
location description its streaming from the map so to do that we'll do something like
will put int…I’m gonna description of the current room…
nextInt…
and print out a message…so let just run this and what will have to do is finish this video
and continue to continue to writing the code but you can see we started out because we
defined location to be equal to 1 default is going to be that object, we put something like we know that
a second location, location 2, on top of a hill, if type 2
on top of a hill, if I type 3 it should be inside a building, 5 your in the forests and
4 lets try the other one as well
Your in a valley and if I type 0 you can see, in front of a computer learning Java which was the exit to break out of it, so that's a very
simple program there, now in the next video what we're going to do is we're going to
enhanced this, we’re going to build the exits, so we’er going to set up so that when you add a
particular location you’ve got a list of exits and you can only use those exits
that are valid for that particular location so let's look at doing that next video