So I continued on with our discussion of collections and
specifically bond research.
You saw in the last video we ended up with this bond research code that came
straight out of the java collections, the source code comes with Java itself.
So I just wanted to reinforce that it's interesting how this does work and
you saw how efficient it was when this little white dot in there to see,
How many iterations were required compared to our version, and
it was significantly more efficient.
So essentially the code,
the algorithms in general that come with java are very efficient.
They have spent a lot of time with it, they've worked hard on it And
it's good to know as a result that we don't have to write our own
versions of these algorithms that are included.
Rather, we can take use of that functionality that's included.
And just to recap that a binary search relies on the list that you're searching,
being sorted.
Now, we saw in an earlier lecture how to use the collections.sort
method to sort a list, but that was providing the items in that list.
Implement the comparable interface.
Now we generated our seat numbers at assorted order, so
there's no need to sort before performing the search, but with that said,
the collection and sort method is available if needed.
And interestingly enough, it uses a very efficient merge sort.
This is the collection sort, and
that performs much better than the bubble sort we used in an earlier challenge Now
as we have already used the sort method we're gonna have a look at the reverse
sort method that the collection class provides sort a list in reverse order.
Now so that we don't mess up the seats list for
our theatre we have to really look at a way to use the lists wireless.
Instructor to copy one list into another.
The collections framework does provide a method to copy lists and
other collections objects, but it doesn't really work as you may expect.
In fact, it'll only copy a list into an existing one, but at least the same size.
We're going to demonstrate the problem after the next few examples Now, so
that we can look at these methods easily in the main method.
What we're gonna do is change the theater class so that seats filled is public, and
the seat class itself is always public And the seat class itself if also public.
So I'm just gonna make that charge.
So I'm gonna change this to public.
So again you wouldn't normally do this will a real program.
But it's just gonna make our testing of these concepts that come
through a bit easier.
So made that public, and also our seats we're going to create that,
convert that I should say, to public instead of it being private.
So that's now an array list as well.
So let's go back now to our main method and
we're just going to delete this content out for now.
And let's add some code so we're gonna put four.
Actually what we'll do is we'll write the print list method first.
So we're gonna create a method that does just that.
Prints out the list.
It's gonna be public static void print
list list as theater .seat.
We're gonna accept the import for Java util list.
That's gonna be list.
Then we'll have a simple 4H list theater.seat seat list.
I'm gonna print out without a space +
seat.getSeatNumber() like so.
Then you're gonna print a line
go to next line then we're going to also put some equal signs there.
To end the line like so.
So what we can now do is create a new array of seat objects and
if we parse in the existing seats from our theater object,
we're going to get a new list containing all the seats.
So the way to do that is we're going to type lists.
Theatre.Seat
> seatCopy = new ArrayList.
And then we do theatre.seats.
And just to confirm that, we now do a print List(seatCopy).
That should confirm that we've got a new list, so
let's try running that to confirm it does work.
And you can see that we've now successfully managed to get our copy.
And scroll right over to the right hand side, but clearly it's gonna go
through and list all the ones How all the entries seats in our array list.
So the seat copy list contains all of the elements that were in the original.
Now, the method that we use to create seat copy and
populate it in all the elements on theater.seats is called a shallow copy.
So what that does is creates an array list containing all the elements from the list
that were passed to the constructor, as you can see.
On this line up here, on line 10.
No note it's very important here to know that these aren't copied.
Hence the term shallow copy.
Now if we modify one of the seats, say by reserving it, for
example,, then that seat will be reserved, which ever list we check for it.
So we may have a new list, but
the contents are a same objects as exists in the theater seats.
So we're taking a copy, a shallow copy Of the same data if you will,
putting that into another array list but
they are effectively the same shared objects and just to demonstrate that is
the case what we're gonna do is we'll add a reservation after here.
So we're going to after the print we'll do something like c copy dot get.
>And we'll just get the second element from your right, docu serve and
we'll just reserve that and obviously that should be the second seat
A1 is the first one A02 would be the second one then we'll do if.
(Theatre.reserveSeat("A02")).
("Please pay for A02").
Else ("Seat already reserved").
So you can see what we've done there,
we've used our copy of the Array list on line 13 to reserve the seat,
the second entry in the array list which should be seat A02, and
then we're going back, reserving the seat from the other array list,
which ordinarily if there was distinct and separate versions of
the objects that should also work, but let's just run it and
confirm what happens You can see, it's come up and said seat already reserved.
So clearly, in fact, even now we've got two separate array lists,
it's very clear that we're sharing the same data because otherwise,
of course, the second reservation would also have worked on the other array lists.
This is an important concept which was also covered
when we discussed link lists which was of course earlier in the course.
So the two separate lists here Each containing references
to the same 96 c object so that's the important thing there.
Separate references but same objects effectively.
So two separate array lists but the references are to the same 96 c type.
To objects that we initially created when we created the theater object
on line nine.
And just to prove that they are separate lists,
we haven't already proved that with what we've done.
Let's just sort one with reverse order then print the other array list out and
see what happens.
So down here, we can do collections.
Collections.reverse and that reverses the order of any list.
So we'll do that for the seatCopy.
And now if I print out, I'll print a message say we're Printing seatCopy.
We're gonna printList().
Seat copy.
Then we'll do another one, printing theater.seats.
The other one.
And for that one, we'll do print list theater.seats.
Clearly, there's two separate array lists that we've got working there.
So let's just run that and confirm what happens.
And you can see here what's happened is, we've got the C copy we'll reverse that.
And we can see now that C copy is in reverse order with starting from
what was the last seat that we created is now in the first position.
But theater.C has still got the original order that was set.
So again, clearly, they are separate array lists But the only thing is that they
refer to the same objects that were initially created on line ten.
There's also a shuffle method in the collections class and that
shuffles the elements of the latest in a random or sort of a pseudo-random order.
So we could go back and try that.
Close that down the run window.
It's a bit of a reverse here.
We can change that to shuffle.
Shuffle.
And if you run that you can see we've got seemingly random order there, T07, E02.
Run that again, AF4, F08, F11, so
that's a Is a good way of pseudo randomization
of a list using the shuffle method that's part of the collections framework.
Obviously there are a lot of other useful methods that form the collections class.
A couple of other useful ones are min and max.
And what they do is return the smallest and
highest elements recorded into their natural sort order.
In other words the sort order that their compare to method maintains.
So we could do something along those lines.
Theatre.
Seat, min seat equals
collections.min seat copy.
Then we can do theater.seat max seat
equals collections.max seat copy.
I could print out a message then min Seat number
is husband seat.get seat number.
Now that it's returning a seat.
And the other one is max seat number is plus max
seat.get seat number.
So if we run this code now.
And predictably the minimum seat is aisle one which was the first element.
And H12 is the max, which of course is the last element in the sorted list.
And obviously it's taking into account that's not necessarily the last element,
it's the one with the high sort order.
And again it uses the compare to method to determine that sort order.
Basing the results on that natural order from the compare to method for
a given list.
And incidentally, these work even if the lister hasn't been sorted.
So we can go about changing that.
So we could change the reference here to theater.seatCopy and
just running that again.
So you can see that is still working even though as you saw with seed copy,
it wasn't sorted.
We used a shuffle method, but it's still going through that entire list and
determining the minimum and the maximum based on the compare to sort order
because that of course will still work out the natural order.
It's just that we haven't sorted the list at this time.
We've used the shuffle method, so minimum maximum will always work,
and is pretty handy if you want to quickly find the minimum and maximum.
Elements in using their natural sort order based on the compare to method.
Now if you'll want to implement your own sort or for any other reason,
the collections class provides a swap method to swap two elements in a list.
So we pass the name on the list and
the index position of the two elements that we wanna swap.
And what happens is the swap method swaps their position in a list.
So what we'll do is we'll create a variation on the bubble sort method from
the arrays challenge.
But this time, we're going to use nested foreloops, and the reason for
that is we had a student in the course ask if that was also acceptable.
Notice that when we start typing this code, actually, I'll type it in first,
then we'll talk about it a little bit more So let's put the code in,
the top of the code in.
It will be public.
Static, void,
sort list.
It's going to be list, extends theater .seat,
list, and we start with a for loop,
for(int i = 0, i < this.size -1,
sorry, and I'll explain this shortly.
i++, and then we put another loop.
The next inner loop is going to be int j = i+1, j < list.size(),
j++ and we put if(list.get(i).compareTo()),
so we're using the compareTo method to determine or
to get a result in integer but.
Back depending whether the entries are less than, the same, or
sort of greater than in the sort order.
They're compared to list.getj.
So we're comparing the two elements.
And if the result of that is greater than zero Zero that means we can swap them,
we wanna swap them to our sorter, that'd be collections.swap, this is how we use
that swap method, list and we need to parse the two elements we wanna swap and
their element positions which is I and j.
Now, just to confirm what we've done there, and again we're using
a variation of the bubble saw method we wrote in the erase challenge.
But again we're using nested four loops and
that's because we had a student ask whether that was also acceptable.
I want you to first notice that the end loop doesn't start at zero But
it actually starts after the current position that the outer loop has reached.
So in other words, the inner loop is changing fewer and
fewer elements each time around.
That's still very much slower than the built in merge sort, but
this could be useful in situations where, for example, speed wasn't so important but
memory was at a premium.
So merge sort requires far more memory than a bubble sort,
and this could be an issue with extremely large lists.
Even so, that said, we'd probably be very rare that you'd want to use your own sort
routine rather than the ones that are built into the collections framework.
Now, like the print list method, which was the method directly above,
by starting on line 34 That had to access the get number seat getter and
therefore only works on c-lists.
What we've done on line 42 is we've made the method generic,
the sort list method generic.
So that can sort any kind of list of theater seats so as long as they implement
the comparable interface and obviously that should be pretty familiar from
the lectures on GenerX where we went through that concept in great detail.
So to call this sort list method, sort list method so
we can see swap in action, we're going to add some code to our main method.
And bearing in mind, we're going to sort the same copy method, and
if you recall on line 21, we shuffled it so
we We know that it's out of order, so let's now try and sort it again.
So we'll go sort list, and it's gonna be sit copy.
Then we'll do a print out, printing.
Sorted seat copy.
And print list so copy so we've sorted it,
we've sent the list to this method to line 46 and we're printing it out, the actual
results and they should then be ordered again so let's go and run that and see.
So I can see that on this line here, below printing copy, at the top,
that we're out of order.
But then down at the bottom, printing sorted order,
you can see clearly now that we're actually sorted correctly.
So our mini sort method worked fine on it.
I'm going to finish this video with a look at a rather odd copy method that
is provided by the collections class.
And what that does is it takes two parameters.
The destination and the source list.
Now the destination has to be of a generic type I there's a super set of the source
list I should say and it basically means it can be collection an iterable or
a list and the source must be a list.
Now the problem that tends to arise with the collections copy method,
is that people either expect to use it as we just did when we used the list
constructor to create our copy of our seats.
We did that all the way up here on line 11.
Or what happens is the people may expect to create a deep copy of list.
As opposed to a shallow copy, a deep copy is a copy where the elements are not just
references To the same elements as in the original list, but are themselves copied.
And of course, that's unlocked the shallow copy that we produced earlier
because we determined when we printed out the results that was the same elements in
the array list that were being effectively duplicated.
In the second array list.
Now I'm gonna copy a collections copy method is gonna file as the destination
is big enough to hold all the elements to be copied into it.
So that actually leads to a problem.
That's pretty typical to see people try to do something like this.
Then knowing that you need to have the same amount of elements,
people will actually type something like list theater.seat
newList = new Array(list).
That would be something like theater.seats.size.
And then collections.copy
ulisttheater.seats.
So in theory, you'd look at that and link it's very common for
people to look out and think.
Well, I've allocated the amount of size for the array list, so I should be able to
run that, but what actually happens is you get an error Source destination.
A source does not fit into destination.
Now, the reason that's fouled is the code on line 37,
what that does, it only sets the capacity of the railist,
giving it the potential to hold that many elements but initially containing none.
So it still doesn't actually create a number of elements.
So to get this to work get the copy to work you actually need to
have 96 seed objects in that new list in that
list that we created on line 37 before we can attempt to copy.
So with that said it's really hard to think of an actual use for
the collections copy method would that would allow all of the elements of a list
to be copied into another collections object providing the object had been
initialized by enough elements first And I guess that's probably why it was included.
Okay. So I'm going to finish the video now,
because we've covered most of the collection's methods that can be used on
lists.
So in the next video,
what I'm going to do is take a look at the other collection types.
See you in that video.