Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

8. Collections > Some Last Thoughts

Some Last Thoughts

This chapter has gone on quite long enough, and I hope that what has come so far has convinced you that the collections in Java are one of the good parts of the library. But before we go on to something else, I need to mention a couple of things.

The first can be seen if we look at the signature of a method we used in our last formatting examples, the addAll() method of TreeSet. Like all of the collections, TreeSet is generic, but this method is generic in a somewhat odd way, with a signature of:

boolean addAll(Collection<? extends E> c)

The argument declaration is something that we haven’t discussed thus far: a wildcard parameter. This parameter says that the argument being fed into the addAll() method has to be a collection, and that collection must be made up of objects that have a type that extends the declared type used to parameterize the TreeSet. In our case, this means that the TreeSet<Player> that we declared can have sets of objects that extend the interface <Player> as well as objects that simply implement exactly that interface. This is because the subtyping relation that holds between classes does not transfer to collections of those classes. To get something like that requires extra syntax in the generics systems. I won’t go into all of the possibilities here, but if you are going to implement a generic collection (as opposed to using such a collection), you will need to consult more advanced writings to understand how this works.


You are currently reading a PREVIEW of this book.


Get instant access to over $1 million worth of books and videos.


Start a Free Trial

  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint