From the course: Learning Java Collections
Unlock the full course today
Join today to access over 23,100 courses taught by industry experts.
Generic typing - Java Tutorial
From the course: Learning Java Collections
Generic typing
- [Instructor] Let's walk through some examples that demonstrate the benefit of using generics with collections. Collections can work with any type of object. So the same type of collection that we use to store strings can also be used to store custom objects like our room. It's not like we need a separate type of collection for the string and another for the room. Like we see in the example, we can use an array list to hold both types. Now collections are only meant to work with objects. So if we insert a primitive like this long, it needs to be autoboxed to its wrapper type to work with the collection. We can see this in action if we stream our collection and then print out the type of its elements. Here I'm just going to get the class of each element in our collection within the stream and print them out. Let's see what happens. You'll notice that our primitive long was autoboxed to its wrapper type java.lang.long.…
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
-
-
-
-
-
-
-
-