Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Java Data Structures!
You have completed Java Data Structures!
Preview
The JCF, or Java Collections Framework. It is a powerful set of tools that you are going to be using for most of your Java adventures from here on out. Let's discuss its history and some of its design decisions.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
In this stage, we'll start exploring
0:05
the very powerful Java Collections
Framework, or JCF.
0:07
Much like the abstraction of storing data
in bits we went over in the first stages,
0:12
this grouping of tools has set out
to do a similar level of abstraction.
0:16
But this time, the abstraction is around
storage and retrieval of data efficiency.
0:21
Now, as you might imagine,
lots of research, trial, error,
0:26
and debating
go into figuring out the quickest way
0:30
to solve a problem
in the most efficient way possible.
0:33
These solutions are called algorithms.
0:37
And while this can be fun
to try and achieve,
0:39
there really is no sense
in recreating the wheel
0:42
over and over again in every project
you work on.
0:44
And out of that thought process,
this framework was created.
0:48
It was made for us
Java developers to embrace
0:51
and not get stuck in the low-level
plumbing of storing and retrieving data.
0:54
The design
0:59
of this framework is simple
and, in my opinion, quite beautiful.
1:00
It makes very heavy usage of interfaces,
so I'm glad we picked those skills up.
1:04
It also provides concrete implementations
1:09
of those interfaces
to use in different situations.
1:11
We'll go over a bunch of these
implementations in the following videos.
1:15
It implements these different interface
methods using that word we learned earlier,
1:19
polymorphism, or many forms.
What these implementations
1:23
based on interfaces do for you
is to allow you to choose at creation time
1:28
the best algorithms
for the way you plan on using the data.
1:33
There are different recommended
algorithms for collections,
1:38
usually based on the size or type of data
they contain.
1:40
Now there's a way of talking
about efficiency in certain algorithms,
1:44
and it might look scary and mathy.
1:48
It's called Big O Notation.
1:50
It helps to find the order of magnitude
required to run the algorithm.
1:52
We'll touch on it super lightly, and I'll
drop some info in the teacher's notes.
1:56
Don't let it scare you.
2:00
That is exactly what this library is here
2:01
for, extracting away
the scary uber brainy stuff.
2:04
If you find that
2:08
you want to go deeper into algorithms,
and there's nothing wrong with that,
2:09
some people's minds love solving those
low-level problems, please have at it.
2:12
Whatever floats your boat.
2:17
And thankfully,
some people do love this stuff,
2:19
and they're the ones that write the
implementations for everyone.
2:22
Now before
we jump in, I want to talk about something
2:26
we mentioned earlier,
and that is generics.
2:28
If you'll recall, we talked
2:31
about typecasting from a superclass
could cause a runtime error
2:32
if we made a mistake or forgot
to use the instance of expression.
2:36
So again, generics were born
out of the need of wanting
2:40
to provide compile time errors
for your type mismatches.
2:43
You're going to start seeing them a lot,
and I don't want it to come as a surprise,
2:47
as it's going to start
introducing some new symbols.
2:51
So here's a quick example.
2:53
When you see the less than symbol
and the greater
2:56
than symbol surrounding a type,
this is a generic.
2:58
This should be read
as a collection of strings.
3:02
Anytime you see the greater than
and less than symbols, read them as of.
3:06
Again, that's collection of strings.
3:10
Now sometimes, the documentation
surrounding
3:13
generics can get a little confusing,
so I'd to walk you through it
3:15
a little bit together
before you run into it on your own.
3:19
Let's go in really quick
and upgrade that comparable interface
3:23
to use generics
instead of plain old objects.
3:26
So again, comparable,
and here's the generic interface.
3:30
So we have the less than
and then the greater than,
3:35
and they're surrounding this T.
3:37
And so here it says type parameters.
3:39
T is the type of object that this object
3:42
may be compared to.
3:44
And if we look down here,
3:48
that T is compared to T, O.
3:49
Now before,
we just used object, and that worked.
3:53
But let's go back
and take a look at the code there.
3:55
So what you want to do is say
what is this is comparable
4:00
to. We're going to put that type in there.
And that type is movie, right?
4:02
We'e saying
4:07
that this is comparable to type movie.
4:08
So if we scroll down here to where
compare to was, now
4:11
we can make this a movie.
4:13
Let's just be clever and we'll call this
other, and we can get rid of this line.
4:19
Because movie's going to come in
for us now.
4:22
Alright, so actually, under the covers,
4:26
the way that generics work is
they do the typecasting logic for you.
4:28
Now it knows things are safe, because
at compile time, the checks are made.
4:32
It does this magic through a process
known as type erasure, and I've included
4:36
a link in the teacher's notes
if you want to dig a little bit deeper.
4:40
Basically, this helps protect ourselves
from runtime errors of casting
4:43
as well as writing the instance of checks
all over the place.
4:47
But here's the good thing.
4:50
It doesn't slow things down at all,
because those changes happen at compile
4:51
time.
4:55
You'll learn to love these generics,
I promise,
4:56
even though they might take
a little bit of getting used to.
4:58
Great.
5:02
Now we won't be shocked by the new symbol
showing up in the code,
5:03
and we learned how to parse
that documentation a little bit better.
5:06
The Collection
Framework's documentation is pretty great,
5:10
and now with that generic knowledge,
it will be a lot more digestible.
5:13
All right, so after this exercise, let's
go take a look
5:17
at our first interface in the collections
framework, and that is the list.
5:20
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up