so far in this section on the Java Collections Framework we looked at the
list interface and its implementations in Array List and linked list we also looked at
the map interface and hash map and the set interface and hash set now there's two
other collection interfaces queue and dequeue that I'm gonna leave to later in the
course so what we gonna do is finish up this section by looking at his variant of the
hash map and hash set classes which is called the linked hash map and linked hash set now their
also sorted versions of the map and set interfaces sorted map and
sorted set that are implemented in tree map and tree set so well actually go through
and have look at those as well now because the operations on sets and maps
are so similar the examples in here will concentrate on the maps rather than sets
the link variants of hash map and hash set are called linked hash map and linked hash set and
any real differences is that they got an ordering to them now in the Oracle
documentation the ordering of items in a Hash Map or Hash set is described as
chaotic but the linked versions maintain insertion order so once you put items in
the collection you know the order that the results will come back when you iterate over it now we'll take
advantage of this in the next example that we gonna start typing up
shortly by adding our items in alphabetical order the examples gonna allows users to add purchases to a
shopping basket after choosing them from the list of all the items on sale now to do
this we need to maintain two collections the first is the list of all the items
are in stock and the second is the items that a customers added to the basket so
although it might seem a bit contrived to add the stock items in alphabetical
order in reality the items would come from a database so they could be at any order
we wanted by using an order by clause and the query the SQL query
that retrieves them from the database so lets get with the example to see how a linked hash map can be
useful and then will ultimately extend that to use a tree map I'm gonna start by
creating a basic stock on item class I'm gonna come over here to my package right
click
Java class and will call it StockItem.....
so in this class we are gonna store the name of the items for sale with a price and the
quantity in stock
also override equals and hash Code and in addition will also implement comparable
so the stock items can be used in a sorted collections so let's go ahead and
start changing this so we need to implement comparable first so.....
...and will get that error until we implement that
method so lets starts some variables so....
.....
all right we need some getters so lets implement some getters first...we do a getter for all 3.....we also one some setters so gonna create the setters and
we'll just change a few little bits and pieces here so.....
.....and names is not coming up
you notice how IntelliJ is smart enough to figure out that name is a final field so as a result it doesn't
come up here and allow us to change it because of course you can't change final fields they
to be initialized either on the line that they declared or in the constructor
so again just another nice thing that IntelliJ of helping us
automatically
so for price I'm just gonna choose price as a setup but we will add one small bit of validation
what we'll do is that we will make sure the price is greater than 0 before you try and save it so......
.....
...then and only then try and save it so if that way if someone passes a price its
less than or equal to 0
we are not gonna save it so instead of having a set of for quantity and stock let's change the method
and we will call that adjust stock because that would make more sense when you will be calling a loop to adjust the stock so....
.....
......
.....
....so just a bit of basic validation the next we want to override equals method then
ultimately will be doing a hash code as well so let's
override that override and by now you should know the commands to override if not refer to some
of the previous videos where I've been through those so do equals first and will get rid of the super because we
actually gonna implementing this
and will put a print out here.....
.....give us some sort of notification on the screen that's happening
first we will do a test to see whether we actually testing against the same object in
memory so......because its actually the same object the same
instance of the object so we don't need to do any other processing because
it's identical its sharing the same area or memory next we need to do a test so....
......
.......
......
.....so obviously equals we know
that they aren't equal for that reason either there's a null because this can't be null
because we're actually in the object running the code so we know
that it's not null so if we are comparing it to null we're going to get a
different value and it's a different class ok we also know that that's going to
be different and then our equals is gonna just use the name at that points so pretty simple and just
for the stock item name and will cast as a stock item
....so we are comparing the name of our current object we're running
in versus the object that's been passed this method and then we are going to return a standard
string equal so....
....so that returns TRUE or FALSE as to whether they are the same so assuming that's the case if it returns
true then we know that the strings are equal and therefore in our case where just
saying the stock item is equal so its our only comparison other than testing to see whether it's a
same object in memory which would return true but we are also then checking the name and of course
on line 49 we are testing to see whether it's null or its a different class
so returning false in that case ok hash code is going to be quite simple
override hash code and that's going to be.......so we are
return a string hash code.....
.....could have been any number I think we use 57 in the previous one previous
set challenge but we could be literally anything their unless it's consistent and we
also need to override compared to so lets do that as well and compared to is overridden from
java.lang.comparable because we are implementing that interface and what we are going to do their
first let's put a message to say......
....first we do a test wet put...
......
....so we are using the in-built functionality for a string in using that
as a way to compare the stock item and the test on line 69 to test its not null we
need to make sure that we do check for null because if we get a null stock item pass
in this method the code on line 65 will still execute because you can compare
null variables just fine but as soon as you start trying to return something like
a method from a string on line 70 will get an error if that's null so that's
why we're checking its not null and then we just using the compared to function of
the strings to compare the stock item name which is in this current object
we're running compared to the one that's been pass to this method that will return the
correct value negative 1 0 or 1 depending on the value whether it's sort of less
than the same or greater than ok so that's returning and then what we'll do is
throw and I know we have not talk too much about exceptions but we are
going to do a throw...and again we will be talking about exceptions a lot more in more detail
later on in the course but for now that's probably not unrealistic to do it
throwing a null pointer exception to say if you want to use to compare to function you have
to assume we're not comparing something that is null ok so that's our compared to function and
last one what we will do is just add a to string will override that as well...
just so we can actually print something out here and we will return.....
....and just to confirm that lines 9 and line 40
we've initialize quantity and stock in both cases and again you wouldn't
normally initialized in both places but the reason I've done this is because
which one to use often confuses
students its really down to personal preference in this case so neither is
better than the other so name and price have to be initialized obviously in the
constructor because we are using the constructors parameters to provide the
values but if your not passing a fields value to the in the constructor then its
doesn't really matter where you initialize it as far as Java is concern so
personal preference you wouldn't do it in both normally but I put up their just
to bring it up for a topic for discussion so just choose one would be my
recommendation and stick to that having said that probably would be useful to
allow the quantity in stock to be set when the stock items are created so we
should really include that field in the Constructors for that reason its also useful
to have a constructor that doesn't include the stock levels with that said
and see if it allows some of the spinner
perhaps to set up the stock items before the list was passed to the warehouse
for the stop to be done so the code for this we are gonna overload the constructor
so the items can be created with or without stock levels so we are going to add a constructor
for both so we will just come back here and add the constructor in all three places
like so and obviously that's now updating it based on the quantity in
stock that has been passed to and that then now that we've done that it makes more
sense here on line 14 to initialize it there because we are also initializing it
on line 20 so as a result we could now removed this line 9 so really
just depends on how are you going to initializing and using the variable
when you define it in the initialize in the constructor like we have or at the
declaration line so I'm gonna remove it their on line 9 for that reason ok so now we've done
that we also did the adjust stock method which I mention was really there to allow our
stock levels to be increased or decreased increase the stock that is added and sold
contains a check to make sure the stock levels can't drop below zero so obviously
we're doing it this way so we can actually pass negative quantities so you
literally adjust the stock and not just adding stock you can also deduct from
it as well and we also overrode the equals and hash Code as well as the compare to
method so I'm gonna finish the video here now in the next video what we'll do is we'll continue on and
we'll actually implement a stock list class and this gonna hold a list of all
the stock items in a map than we will need some methods to add stock and to remove it
when it is sold and we will be doing that in the next video