1 / 16

COMP 121

COMP 121. Week 8: Generic Collections. Objectives. To understand type variables and how they are used in generic programming To be able to implement and use generic classes and methods To introduce generic collections and the Java Collection hierarchy. Type Variables.

ggarza
Download Presentation

COMP 121

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP 121 Week 8: Generic Collections

  2. Objectives • To understand type variables and how they are used in generic programming • To be able to implement and use generic classes and methods • To introduce generic collections and the Java Collection hierarchy

  3. Type Variables • Generic programming: creation of programming constructs that can be used with many different types • In Java, achieved with inheritance or with type variables • For example: • Type variables: Java's ArrayList (e.g. ArrayList<String>) • Inheritance: Using Object to enable a data structure to store any type of object • A Generic class is declared with a type variable • In an ArrayList, the type variable denotes the element type • public class ArrayList<E> • { public ArrayList() { . . . } public void add(E element) { . . . } . . . } Horstmann, C. (2008). Big Java (3rd ed.). New York: John Wiley & Sons.

  4. Type Variables (cont’d) • Must be instantiated • The actual type must be supplied when you use the generic class • Can be instantiated with class or interface types ArrayList<BankAccount>ArrayList<Measurable> • Cannot use a primitive type as a type variable ArrayList<double> // Wrong! • Use corresponding wrapper class instead ArrayList<Double> • Supplied type replaces type variable in class interface • Type variables make generic code safer and easier to read Horstmann, C. (2008). Big Java (3rd ed.). New York: John Wiley & Sons.

  5. Instantiating a Generic Class GenericClassName<Type1, Type2, . . .> Example: ArrayList<BankAccount>HashMap<String, Integer> Purpose: To supply specific types for the type variables of a generic class. Horstmann, C. (2008). Big Java (3rd ed.). New York: John Wiley & Sons.

  6. Generic Methods • Generic method: a method with a type variable • Can be defined inside ordinary and generic classes • The type variables of a generic method are specified between the modifiers and the method return type • No need to instantiate the type variables Horstmann, C. (2008). Big Java (3rd ed.). New York: John Wiley & Sons.

  7. Generic Methods (cont’d) • Non-generic method to print all the String elements in an array public static void print(String[] a) { for (String e : a) System.out.print(e + " "); System.out.println(); } • Generic method to print all the elements in an array public static <E> void print(E[] a) { for (E e : a) System.out.print(e + " "); System.out.println(); } Horstmann, C. (2008). Big Java (3rd ed.). New York: John Wiley & Sons.

  8. Conventions for Type Variable Names Horstmann, C. (2008). Big Java (3rd ed.). New York: John Wiley & Sons.

  9. Generic Collections • Generics allow you to define a collection that contains references to objects of a specific type ArrayList<String> myList = new ArrayList<String>(); specifies that myList is a List of String where String is a type parameter • Only references to objects of type String can be stored in myList, and all items retrieved would be of type String • A type parameter is analogous to a method parameter Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  10. Creating a Generic Collection Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  11. The Collection Hierarchy Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  12. Common Features of Collections • Collection interface specifies a set of common methods • Fundamental features include: • Collections grow as needed • Collections hold references to objects that can be accessed using an Iterator object • Collections have at least two constructors (one to create an empty collection and one to make a copy of an existing collection) • Collections override equals, hashCode, and toString (inherited from Object) in a reasonable way that includes the elements they contain • Collections are considered “unordered,” meaning that there is no guarantee about where in a collection an added element will be placed Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  13. Some Collection Methods Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

  14. Summary • In Java, generic programming can be achieved with inheritance or with type variables • A generic class has one or more type variables • Type variables can be instantiated with class or interface types • Type variables make generic code safer and easier to read • Type variables of a generic class follow the class name and are enclosed in angle brackets Horstmann, C. (2008). Big Java (3rd ed.). New York: John Wiley & Sons.

  15. Summary (continued) • Type variables can be used for fields, method parameters, and return values • Type variables of a generic method are specified between the modifiers and the return type • Type variables do not need to be instantiated when calling a generic method • Generics are used in the Java Collection framework to define a collection that contains references to objects of a specific type Horstmann, C. (2008). Big Java (3rd ed.). New York: John Wiley & Sons.

  16. Any Questions?

More Related