(upbeat music)
- [David] The Java collections framework
is a set of interfaces and classes that make it easy
to manage data collections in your applications.
You can manage either ordered or unordered data
and you can use one of four categories of classes.
Each of these categories is represented by an interface.
On this page they're shown in the far left column
and they're named set, list, deque and map.
And then across the top, you'll see the different types
of collections within the categories.
So for example, let's say you wanted to create a list.
A list is ordered data, and you wanted it to be resizable.
You might then select the ArrayList class.
Now, when you declare a collection object
it's traditional to set the type of the collection
as the interface.
And then when you instantiate the object
you use a construct or method for the concrete class
that you want to use.
So let's start in this project where the main method
is empty, but I have a single object called clothing item.
I'm going to start by creating a list that contains strings.
Using collections will also introduce you
to the concept of generics.
Generic syntax makes the use of collections type safe.
That is you can trust that the items in the collection
are of a particular type.
If I want to create a list of strings,
I would start with what's known as the diamond operator,
and then I declared the type of the items within the list
in the diamond.
Now the list interface has to be imported.
I'll move the cursor after the identifier
and press Control + Space.
And I'll choose list from java.util.
I'll name my list states.
Next I'll initialize it with new.
And then I have to use a constructor method
from one of the concrete classes.
I couldn't for example say I wanted to create a new list,
interfaces don't have constructors.
So instead I might use ArrayList,
and that's all there is to it.
Now in older versions of Java, you would have also had
to declare the type of the items in the list
in the diamond operator in the constructor.
So you might've done something like this,
but that's no longer necessary.
Starting in Java 7 you can eliminate
the secondary declaration.
And in fact, IntelliJ IDEA shows you
that that type is dimmed out.
And when I use an intention action,
it offers to replace that with just the diamond operator.
Now, the ArrayList is a resizable array.
So now I can add as many items as I want.
Use the syntax states.add and then I'll parse in a string.
I'll parse in California.
Then I'll duplicate that line a few times.
And for the second one I'll use Oregon.
And for the third one I'll use Washington.
Then as I did before with the array,
I'll use a for each loop.
And then each item in the list is a string,
I'll call it state, and I'll get these values
from the states collection.
And then I'll output the current state.
And there's the result.
The items are stored in the order in which I declared them.
Let's say now that you wanted to create a list
of numeric values.
You might try something like this, list with a type of int,
but that won't work.
The items within a collection must be objects,
not primitives.
So if you want to create a list of numeric values,
you would use the helper class.
I'll use integer, I'll call the list int for integers,
and I'll instantiate it using an exactly
the same syntax as before.
I'll start adding numeric values.
Then I'll do a loop using the for each syntax.
For each integer within the loop,
I'll output it to the Console.
And again, the data is stored in the order
in which I declared it.
If I want to remove an item, I can do it easily
using a method of the list interface called remove.
So for example, I'll use states.remove
and I'll parse in a value of one.
And remember that indexing starts at zero in Java,
so that really means remove the second item.
And when I run the code now,
my list still contains the first and third item
but the third item is now the second item.
If I want to retrieve an item in a particular location,
I can use a method of the list interface called get.
I'll create a string called state and I'll get its value
with states.get, and then I'll parse in a value of one.
And then I'll output the second state is,
and then I'll append state.
And there's the result.
The second state is Washington.
And if I want to find a particular item in the list,
I can do that with a method of the list interface
called indexOf.
I'll declare an integer that I'll name pos for position
and I'll get its value with states.indexOf
and I'll parse in a value of Washington.
And then I'll output the string,
Washington is at position,
and then I'll output that variable.
There are many more methods you can use
in the list interface.
I'll click into the list identifier and press Shift + F1,
and that takes me to the documentation.
Then I'll go down to the method list
and you'll see all sorts of things you can do,
to add, remove, inspect
and otherwise manipulate lists of data.
(upbeat music)