课程: Learning Java Collections

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

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.…

内容