From the course: Learning Java Collections

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Creating a collection

Creating a collection - Java Tutorial

From the course: Learning Java Collections

Creating a collection

- [Instructor] Let's walk through a few code examples that create a collection. Collections can be declared and instantiated like any other object. We specify the type of the collection, provide its identifier, and then instantiate a new collection implementation that will be assigned to the variable. I'm using the collection interface as the variable type. However, if I wanted to be more specific about the type, I could've switched over to use the set interface. So let's make the change and import the set from java.util.set. Now, the reason this works is because the hash set implements the set interface. We couldn't do something like using a list as our variable type because it's not compatible with hash set. But let's go ahead, we'll import the list from java.util.list, and then we'll change the implementation over to use the array list. So we'll add the array list constructor, and now we have a list. Now, let's…

Contents