There's just a few little things now, to finish off in our generics code,
and then we're actually going to move on to the challenge.
So in the last video, I just showed you how to actually change the team
match result method, right here so, we could actually use that
type to actually confirm, that any plays of the appropriate type.
Any teams, I should say, of the appropriate type could call the method.
And so, consequently in match result,
when we're calling that from the Adelaide Crows,
we can't actually pass a baseball team to it, because we've defined Adelaide Crows
to actually be a team of type Football Player, so therefore,
that baseball team isn't going to work, so I'm going to comment that out now.
Now, the team class includes a very simple ranking.
I think, I talked very briefly about that.
And the ranking was just a very simple formula to
calculate how this team is ranking in this league.
And it's the number of games won multiplied by two,
plus the number of games tied., So, pretty simple.
So, in a real world.
Obviously, league the ranking would also consider the number of games played, and
things like the goal difference, and perhaps other things as well.
And obviously, you're free to play more complexity to the myth if you're interest
in doing these things yourself.
And having a better of play around.
But, we're going to leave that fairly simple for
this exercise as we move forward.
So, let's actually go ahead now, and actually, print these rankings out.
And that's actually, quite simple to do.
So, we can just come in here, and do some printout, or print out some items.
We can say rankings.
Then we could do Adelaide Crows.getName plus column,
and plus AdelaideCrows.ranking.
LIkewise, we're gonna do the same for Melbourne.
Melbourne.get name plus colon space plus Melbourn.ranking.
Next for Fremantle.
Freemantle, is a strain for [INAUDIBLE] get names
plus call space plus freemantle.ranking.
And lastly, that's all.
It wants to definitely run the 2015 [INAUDIBLE] for Australia.
Plus a space like so.
And it'll be hawthorn.ranking.
So, we can actually now, run that.
And we can actually, see the Adelaide Crows have got four points.
Then, if we have a look we can see, sort of see why.
Melbourne and Freemantle have got zero points, because we haven't actually,
defined them as having any results essentially.
It's only Freemantle, and LA Crows that have actually got some results in there,
and that's where I've got two points, and you can see that's obviously,
from the various metrics aren't set with actually into.
So, that's all very well.
But one thing we can't do is compare things,
to see which would be the highest in the league table.
So, let's make a change to that.
Let's actually, make a change to our team class
to implement something that's very useful in interface called comparable.
So, we close down the run window, and go back to the team class.
We need to actually, change the definition at the top.
We've obviously, got extending player.
We need to then, implements, and implements comparable.
Comparable.
And, we need to actually use the diamond, again, so T again because obviously,
we want to make sure that we are still using the same generics like that.
And obviously, we are getting an error now, because it's saying we must implement
the abstract method compare to in order to, actually fulfill this interface.
And you can see that, there's literally only one thing to actually change.
One method we need to do to actually fulfil the interface, and
that's the compare to function.
Because we only want to compare our teams, to other teams,
that's why we're actually using the team and the type.
So, it doesn't make sense to compare a football team to a baseball team.
So, that's why we're actually adding team, and
then, the actual type, in another set of diamonds.
So, the infamous part looks complicated, but in essentially what we're doing is
we've created the ability to have generic types, such as team football player.
So, all we're actually doing here is specifying a generic type as a parameter,
to the generic interface comparable.
So, team is the type in this case, and team football player, or
team T as you got here, is also a type.
So, comparable T in this case could actually accept either.
So, the compare to method that we're about to write,
which is defined by the comparable interface that we're implementing up here,
takes an objec,t and returns a negative number if this is less than the object.
Zero, if they're equal, and a positive if this is greater than the object.
So here,what I'm talking about is less than these that we should sort
earlier then.
And ask if we want teams with higher rankings to sort higher up the list.
We need to make our compare to return less if the ranking,
is greater than the opponents.
I hope that actually makes sense.
We've actually used the comparative method when we implemented our
own link list in a previous challenge.
And many of the Java classes,
actually implement this [INAUDIBLE] including the string in integer classes.
That's how these objects can be sorted as a result of doing that.
But we're doing is we're going to be writing our own code,
because of course we used the string compare to in the previous challenge,
by using the compare, comparable or interface.
And, we're gonna be implementing the compare two method,
which takes the parameter of T, Team, the type.
Type parameter to compare.
And again, we're doing that so, we don't try and compare football, and
baseball teams for argument sing.
So, if we had just changed that so, that was just literally team, like this.
Compare to it, and could only take 10 as its parameter.
Then, when we write the method, compare to,
it will also be able to take team as its parameter.
And, we wouldn't have that ability to ensure that only the types of teams,
in this case, that are appropriate, can be parsed to that method.
So, I'm gonna leave that back in so, let's go ahead, and
actually write this compare to interface.
So, we come down here, under ranking.
And I'm going to implement.
Implement the compare T method.
And you can see, how it's automatically looked at the definition, and it has added
a similar definition, now I'm going to change the default of zero to team,
because that's essentially what it is in this case.
It's the team.
And what we now, need to do is go through.
and do this test like I've said.
So, the first test would be, if this dot ranking, so
this is the current object we're comparing.
If it's greater than what's been passed to us,
the team dot ranking, team dot ranking.
Remember, the higher the score the better.
Then, you're gonna return negative one.
So, in other words, the current team, the one that we're actually in,
this class in other words, or this instantiation of the class, if the team
ranking for that is higher than the team that's been passed to this method,
then it means that this is actually higher in essence, and we're going to return -1.
That's the first one, otherwise we're gonna put else if(this.ranking
If it's less than team.ranking, so is the other way around,
we can have return 1 if the opposite is true.
The current object hasn't got more points, essentially, it's got less points so
we're gonna return 1.
And otherwise, we're gonna return 0 cuz it
obviously means that the rankings are actually the same.
So, that's what we need to do to implement that.
And then, we can get back to main Java.
Code, and we can actually int that up.
So, we can do something like AdelaideCrows.compareto, and let's just
say Melbourne and Melbourne I believe had 0 points and.
So, AdelaideCrows compare to.
Let's just try, those two on them.
So, you can see in this case, Adelaide Crows has got four points, and
comparing to Hops and Melbourne, which has got 0 points, it returns -1,
because we've got a higher ranking, in this case, than Melbourne.
And likewise, compared to Hawthorn, we've also got a higher ranking.
But, if we actually wanted to do a test,
let's actually compare Hawthorn to the Adelaide Crows, so
we'll do it the other way around, just to confirm it's still working.
We'll put, and Hawthorn compare to LA Crows.
We should get the opposite, because we're now comparing Hawthorn to the LA Crows
from the other point of view, but also that's the one that's the same.
We'll do, Melbourne dot compare to Fremantle by which both should return 0,
so that we should actually get 0, because they're both the same score.
So, we run those.
So, you can see in the third example, now ,we've got Hawthorn compared to Adelaide.
Adelaide's got more points, Hawthorn had less, so we're gonna returned 1 there.
And in the last example,
melbourne.compare to(fremantle) they both actually have 0 points.
So, obviously.
Compare return 0.
So, that's actually working, which is good.
Now, the reason we've implemented the comparable interface,
other than to show you how to use it, because I think it's actually very useful.
But also is going to be to help you, with the challenge to come in the next video.
So, if you've got a list of objects that implement comparable,
there such as team for example.
What you can do is you can quickly sort the lis,
by using the static sort method of the collections class.
I'm gonna talk more about that, and give you a little bit of a hint
in the next video when we're actually gonna go through the challenge.
But essentially,
if you've got an array list of teams you can use this code, something like this.
Sudo code would be, if you had something like an array list of teams like so,
you could do collections.sort(teams) and that would be sorted.
And the whole point about this is it will actually be sorted,
using the compare to function that we actually wrote for the actual team itself.
So i,n other words this method would be actually automatically utilized
by the actual code when we were actually doing the ".sort".
And we're just getting an error there, because we haven't actually initialized
it, it is just purely to give you a bit of an overview of what it can do.
Now ,that's obviously one way to do the sort, but you can obviously go back and
refer to the sort method that we actually wrote for the arrays challenge as well.
But then, you can see this is quite simple actually using the built-in
Java collections class.
And we've got an entire section of that about collections
in upcoming videos showing how to use that.
But this is just a simple way of doing it.
But anyway, I will talk more about that in the next video.
I'm going to start this one now.
There is probably no point in me starting the challenge in this video.
What I will do is I like to start the challenge in the next video, and
we will go through what's required and actually talk about the solution.
So, I will see you in the next video.