Lec07 - Generics & Collections.pdf - Unauthorized...

This preview shows page 1 - 6 out of 36 pages.

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Generics and CollectionsPeter Muturi ([email protected]ฺacฺke) has a non-transferable license touse this Student GuideฺUnauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ
Java SE 7 Programming 7 - 2Copyright © 2012, Oracle and/or its affiliates. All rights reserved.ObjectivesAfter completing this lesson, you should be able to:Create a custom generic classUse the type inference diamond to create an objectCreate a collection without using genericsCreate a collection by using genericsImplement an ArrayListImplement a SetImplement a HashMapImplement a stack by using a dequeUse enumerated typesPeter Muturi ([email protected]ฺacฺke) has a non-transferable license touse this Student GuideฺUnauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ
Java SE 7 Programming 7 - 3Copyright © 2012, Oracle and/or its affiliates. All rights reserved.GenericsProvide flexible type safety to your codeMove many common errors from runtime to compile timeProvide cleaner, easier-to-write codeReduce the need for casting with collectionsAre used heavily in the Java Collections APIPeter Muturi ([email protected]ฺacฺke) has a non-transferable license touse this Student GuideฺUnauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ
The two examples in the slide show very simple caching classes. Even though each class is very simple, a separate class is required for any object type.Java SE 7 Programming 7 - 4Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Simple Cache Class Without Genericspublic class CacheString { private String message = "";public void add(String message){this.message = message;}public String get(){return this.message;} }public class CacheShirt {private Shirt shirt;public void add(Shirt shirt){this.shirt = shirt;}public Shirt get(){return this.shirt;} }Peter Muturi ([email protected]ฺacฺke) has a non-transferable license touse this Student GuideฺUnauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ
To create a generic version of the CacheAnyclass, a variable named Tis added to the class definition surrounded by angle brackets. In this case, Tstands for “type” and can represent any type. As the example shows, the code has changed to use tinstead of a specific type information. This change allows the CacheAnyclass to store any type of object.Twas chosen not by accident but by convention. A number of letters are commonly used with generics. Note: You can use any identifier you want. The following values are merely strongly suggested.Here are the conventions:T: TypeE: ElementK: KeyV: ValueS, U: Used if there are second types, third types, or moreJava SE 7 Programming 7 - 5Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Generic Cache Class1 public class CacheAny <T>{23private T t;45public void add(T t){6this.t = t;7}89public T get(){10

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture