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

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