- A generic type is a generic class or interface that is parameterized over types.
public class Box<T> {
private T t;
public void set(T t) { this.t = t; }
public T get() { return t; }
}
E - Element (used extensively by the Java Collections Framework)
K - Key
N - Number
T - Type
V - Value
S,U,V etc. - 2nd, 3rd, 4th types
public interface Pair<K, V> {
public K getKey();
public V getValue();
}
public class OrderedPair<K, V> implements Pair<K, V> {
private K key;
private V value;
public OrderedPair(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() { return key; }
public V getValue() { return value; }
}
MyClass<Foo> my = new MyClass<Foo>();
Foo foo = new Foo();
Foo newFoo = my.doSomething(foo);
Foo newFoo = (Foo) my.doSomething(foo);