Skip to content

Instantly share code, notes, and snippets.

@mathrik
Created March 7, 2014 19:55
Show Gist options
  • Save mathrik/9418699 to your computer and use it in GitHub Desktop.
Save mathrik/9418699 to your computer and use it in GitHub Desktop.
Lesson on Java Generics from Oracle Java Tutorials
public class Box<T> {
private T t;
public void set(T t) {
this.t = t;
}
public T get() {
return t;
}
public <U extends Number> void inspect(U u){
System.out.println("T: " + t.getClass().getName());
System.out.println("U: " + u.getClass().getName());
}
public static void main(String[] args) {
Box<Integer> integerBox = new Box<Integer>();
integerBox.set(new Integer(10));
integerBox.inspect("some text"); // error: this is still String!
}
}
// <T extends B1 & B2 & B3>
// What type of argument does it accept?
public void boxTest(Box<Number> n) { /* ... */ }
// Are you allowed to pass in Box<Integer> or Box<Double>?
// http://docs.oracle.com/javase/tutorial/java/generics/inheritance.html
public interface Comparable<T> {
public int compareTo(T o);
}
public static <T extends Comparable<T>> int countGreaterThan(T[] anArray, T elem) {
int count = 0;
for (T e : anArray)
if (e.compareTo(elem) > 0)
++count;
return count;
}
Object someObject = new Object();
Integer someInteger = new Integer(10);
someObject = someInteger; // OK
public void someMethod(Number n) { /* ... */ }
someMethod(new Integer(10)); // OK
someMethod(new Double(10.1)); // OK
Box<Number> box = new Box<Number>();
box.add(new Integer(10)); // OK
box.add(new Double(10.1)); // OK
public class Util {
// Generic static method
public static <K, V> boolean compare(Pair<K, V> p1, Pair<K, V> p2) {
return p1.getKey().equals(p2.getKey()) &&
p1.getValue().equals(p2.getValue());
}
}
public class Pair<K, V> {
private K key;
private V value;
// Generic constructor
public Pair(K key, V value) {
this.key = key;
this.value = value;
}
// Generic methods
public void setKey(K key) { this.key = key; }
public void setValue(V value) { this.value = value; }
public K getKey() { return key; }
public V getValue() { return value; }
}
Pair<Integer, String> p1 = new Pair<>(1, "apple");
Pair<Integer, String> p2 = new Pair<>(2, "pear");
boolean same = Util.<Integer, String>compare(p1, p2);
// same as above using inference
Pair<Integer, String> p1 = new Pair<>(1, "apple");
Pair<Integer, String> p2 = new Pair<>(2, "pear");
boolean same = Util.compare(p1, p2);
/*
The most commonly used type parameter names are:
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; }
}
OrderedPair<String, Integer> p1 = new OrderedPair<>("Even", 8);
OrderedPair<String, String> p2 = new OrderedPair<>("hello", "world");
public class Box {
private Object object;
public void set(Object object) { this.object = object; }
public Object get() { return object; }
}
/**
* Generic version of the Box class.
* @param <T> the type of the value being boxed
*/
public class Box<T> {
// T stands for "Type"
private T t;
public void set(T t) { this.t = t; }
public T get() { return t; }
}
static <T> List<T> emptyList();
List<String> listOne = Collections.emptyList(); //this works
List<String> listOne = Collections.<String>emptyList(); // not necessary to include the type witness
Map<String, List<String>> myMap = new HashMap<String, List<String>>();
same as
Map<String, List<String>> myMap = new HashMap<>();
// this doesn't work though
Map<String, List<String>> myMap = new HashMap(); // unchecked conversion warning
class MyClass<X> {
<T> MyClass(T t) {
// ...
}
}
MyClass<Integer> myObject = new MyClass<Integer>("")
MyClass<Integer> myObject = new MyClass<>(""); // can also do this with Java 7
Note: It is important to note that the inference algorithm uses only invocation arguments, target types, and possibly an obvious expected return type to infer types. The inference algorithm does not use results from later in the program.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment