Download COMP 121 Week 8

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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

Generic programming: creation of programming
constructs that can be used with many different types


For example:



In Java, achieved with inheritance or with type variables
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.
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.
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.
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.
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.
Conventions for Type Variable Names
Type Variable Name Meaning
E
Element type in a collection
K
Key type in a map
V
Value type in a map
T
General type
S, U
Additional general types
Horstmann, C. (2008). Big Java (3rd ed.). New York: John Wiley & Sons.
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.
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.
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.
Common Features of Collections


Collection interface specifies a set of common
methods
Fundamental features include:
 Collections
 Collections
grow as needed
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.
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.
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.
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.
Any Questions?