Java Reference
In-Depth Information
BankAccount account = result.getSecond();
This class can be useful when you implement a method that computes two values at
the same time. A method cannot simultaneously return a String and a
BankAccount , but it can return a single object of type Pair<String,
BankAccount>.
The generic Pair class requires two type variables, one for the type of the first
element and one for the type of the second element.
We need to give names to the type variables. It is considered good form to give short
uppercase names for type variables, such as the following:
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
Type variables of a generic class follow the class name and are enclosed in angle
brackets.
You place the type variables for a generic class after the class name, enclosed in angle
brackets ( < and > ):
public class Pair<T, S>
768
769
When you define the fields and methods of the class, use the type variable T for the
first element type and S for the second element type:
public class Pair< T , S >
{
public Pair( T firstElement, S secondElement)
{
first = firstElement;
second = secondElement;
}
public T getFirst(){ return first; }
public S getSecond(){ return second; }
Search WWH ::




Custom Search