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 - 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
-
-
-
-
Interfaces6m 58s
-
(Locked)
Collection types2m 52s
-
(Locked)
The Collection interface2m 4s
-
Collection methods4m 24s
-
(Locked)
Creating a collection5m 4s
-
(Locked)
Generic typing4m 26s
-
(Locked)
Object comparison2m 19s
-
(Locked)
The equals method6m 40s
-
(Locked)
Challenge: Adding and removing collection elements5m 6s
-
(Locked)
Challenge: Advanced collection problems6m 48s
-
-
-
-
-
-
-
-