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
Help

Chapter 4: Data Structures, Conditionals... > 4-5. Using Generic Types

4-5. Using Generic Types

Problem

You are a new Java developer and you need to come up to speed quickly on on generics, or you are an experienced Java developer and you want to learn about a new Java 7 feature that will make your generics programming easier.

Solution

You’ll likely first encounter generic types when using the interfaces and classes that are part of the Java Collections Framework (http://download.oracle.com/javase/tutorial/collections/). The Collections Framework makes heavy use of Java generics. All collection types are parameterized to allow you to specify, at the time of instantiation, the type of elements the collection can hold. The example code in this recipe will be less of a “recipe” and more of a demonstration of the need-to-know topics of generics that will get you up to speed quickly. The examples will demonstrate the use of generics with Java collections versus showing you how to create generic types. Unless you are developing a library API, you probably won’t be creating your own generic types. However, if you understand how generics are used with the Collection interfaces and classes, you will have the knowledge you need to create your own generic types. The source code for this recipe is contained in GenericsDemo.java.


  

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