I'm writing functional-style static helper methods acting as operators for a generic abstraction (say Iterable<T>
), and I'm a bit confused about when I should use wildcards. What are the correct, most type-safe and simplest method signatures in the following cases, and why?
- Checking properties:
public static int size(Iterable<?> source)
vs.
public static <T> int size(Iterable<T> source)
- Transforming:
public static <T> Iterable<T> take(Iterable<T> source, int count)
vs.
public static <T> Iterable<T> take(Iterable<? extends T> source, int count)
- Combining:
public static boolean elementsEqual(Iterable<?> first, Iterable<?> second)
vs.
public static <T> boolean elementsEqual(Iterable<T> first, Iterable<T> second)
vs.
public static <T> boolean elementsEqual(Iterable<? extends T> first, Iterable<? extends T> second)
source, int count)` which lets the compiler infer the type arguments for `Iterable nums = take(ints, 3);`.