•
You can also use generic types to declare generic
methods.
•
A generic method containing one or more type
parameters affects that method only.
•
A non-generic class can contain a mixture of generic
and non-generic methods.
•
To invoke a generic method you prefix the method
name with the actual type in angle brackets.
public static <E> void print(E[] list)
GenericMethod.<String>print(strings);
Generic Methods
Example: Generic Method
The following program declares a generic method print to print an array of objects. First an array of integer objects is passed to invoke the generic print method, and then print is invoked with an array of strings:
public class GenericMethod {
public static void main(String [] args) { Integer[] integers = {1,2,3,4,5};
String[] strings = {"Dracula", "Titanic", "Deliverance", "Casablanca"};
GenericMethod.<Integer>print(integers); GenericMethod.<String>print(strings); }
public static <E> void print(E[] list) {
Oracle University and Global Business Solutions Peru SAC use only
THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED
Generic Methods (continued)
for (int i = 0; i < list.length; i++)
System.out.print(list[i] + " ");
System.out.println();
}
}
Example: Generic Method Used With a Collection
This example uses the generic max method to compute the greatest value in a collection of elements of an unknown type A.
class Collections {
public static <A extends Comparable<A>> A
max(Collection< A > xs) { Iterator< A > xi = xs.iterator(); A w = xi.next(); while (xi.hasNext()) { A w = xi.next(); if (w.compareTo(x) < 0) w = x; } return w; } }
The max method has one parameter named A. It is a placeholder for the element type of the collection that the method works on. The type parameter has a bound; it must be a type that
is a subtype of Comparable<A>.
Oracle University and Global Business Solutions Peru SAC use only
THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED
Oracle Fusion Middleware 11g: Java Programming 12 - 9
Copyright © 2009, Oracle. All rights reserved.
Wildcards
•
The character ? is a wildcard character.
•
?
stands for any Java type, a placeholder that can have
any type assigned to it.
•
List<?>
indicates a list which has an unknown object
type.
•
The use of wildcards is necessary because objects of
one type parameter cannot be converted to objects of
another parameter.
List<?> anyObjects = null;
Wildcards
Generic type parameters in Java are not limited to specific classes. Java allows the use of wildcards to specify bounds on the type of parameters a given generic object may have. Wildcards are type parameters of the form “?.”
Because the exact element type of an object with a wildcard is unknown, restrictions are placed on the type of methods that may be called on the object. As an example of an unbounded
wildcard, List<?> indicates a list that has an unknown object type. Methods that take such a list as an argument, can take any type of list, regardless of parameter type. Reading from the list will return objects of type Object, and writing non-null elements to the list is not allowed, since the parameter type is not known.
To specify the upper bound of a generic element, the extends keyword is used, indicating that the generic type is a subtype of the bounding class. Thus it must either extend the class, or implement the interface of the bounding class. So List<? extends Number> means that the list contains objects of some unknown type that extends the Number class; for example, the list could be List<Float> or List<Number>.
The use of wildcards is necessary since objects of one type parameter cannot be converted to objects of another parameter. Neither List<Float> nor List<Number> is subtype of the other (even though Float is a subtype of Number). So code that deals with List<number> does not work with List<Float>. The solution with wildcards works because it disallows operations that would violate type safety.
Oracle University and Global Business Solutions Peru SAC use only
THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED
Wildcards (continued)
To specify the lower bound of a generic element, the super keyword is used, indicating that the generic type is a supertype of the bounding class. So List<? super Number> could be List<Number> or List<Object>. Reading from the list returns objects of type Object. Any element of type Number can be added to the list since it is guaranteed to be a valid type to store in the list.
Example: Using a Generic Method and Wildcards
The following method prints out all the elements in a collection. Note that the Collection<?>is the collection of an unknown type.
void printCollection(Collection<?> c) { for(Object o:c) {
System.out.println(o); }
}
Oracle University and Global Business Solutions Peru SAC use only
THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED
Oracle Fusion Middleware 11g: Java Programming 12 - 11
Copyright © 2009, Oracle. All rights reserved.