Monday, May 28, 2012

Recursive Generic definitions in Java

I can think of two recursive Generic definitions common in java:
1. For Comparable types -
<T extends Comparable<T>>

Using this a sort API would look something like this:
<T extends Comparable<? super T>> void sort(T[] a);


2. For Enum types:
<E extends Enum<E>>

Using this a utility function in EnumSet looks like this:
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2)

No comments:

Post a Comment