Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— 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 )  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 Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons. public class ArrayList { public ArrayList() {... } public void add(E element) {... }... }

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 ArrayList  Cannot use a primitive type as a type variable ArrayList // Wrong!  Use corresponding wrapper class instead ArrayList Supplied type replaces type variable in class interface Type variables make generic code safer and easier to read Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

5 Instantiating a Generic Class GenericClassName Example: ArrayList HashMap Purpose: To supply specific types for the type variables of a generic class. Horstmann, C. (2008). Big Java (3 rd 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 (3 rd 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 void print(E[] a) { for (E e : a) System.out.print(e + " "); System.out.println(); } Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

8 Conventions for Type Variable Names Type VariableName Meaning EElement type in a collection KKey type in a map VValue type in a map TGeneral type S, UAdditional general types Horstmann, C. (2008). Big Java (3 rd 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 myList = new ArrayList (); 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 (3 rd 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 (3 rd ed.). New York: John Wiley & Sons.

16 Any Questions?


Download ppt "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."

Similar presentations


Ads by Google