Item 30: Favar generic methods

30. Select a generic method

// Uses raw types - unacceptable! (Item 26)
public static Set union(Set s1, Set s2) {
    Set result = new HashSet(s1);
    result.addAll(s2);
    return result;
}

To remove the warning and make it type-safe, write the type parameter between the method qualifier and the return value of the method as follows.

// Generic method
public static <E> Set<E> union(Set<E> s1, Set<E> s2) {
    Set<E> result = new HashSet<>(s1);
    result.addAll(s2);
    return result;
}
package tryAny.effectiveJava;

import java.util.function.UnaryOperator;

public class GenericsTest7 {
    public static void main(String[] args) {
        String[] strings = { "jute", "hemp", "nylon" };
        UnaryOperator<String> sameString = identityFunction();
        for (String s : strings)
            System.out.println(sameString.apply(s));
        Number[] numbers = { 1, 2.0, 3L };
        UnaryOperator<Number> sameNumber = identityFunction();
        for (Number n : numbers)
            System.out.println(sameNumber.apply(n));

    }

    // Generic singleton factory pattern
    private static UnaryOperator<Object> IDENTITY_FN = (t) -> t;

    @SuppressWarnings("unchecked")
    public static <T> UnaryOperator<T> identityFunction() {
        return (UnaryOperator<T>) IDENTITY_FN;
    }

}
public interface Comparable<T> {
    int compareTo(T o);
}
// Using a recursive type bound to express mutual comparability
public static <E extends Comparable<E>> E max(Collection<E> c);

<e extends comparable<e>>Represents "every element e comparable to itself". The following is the max method using recursive boundaries.

package tryAny.effectiveJava;

import java.util.Collection;
import java.util.Objects;

public class GenericsTest8 {
    // Returns max value in a collection - uses recursive type bound
    public static <E extends Comparable<E>> E max(Collection<E> c) {
        if (c.isEmpty())
            throw new IllegalArgumentException("Empty collection");
        E result = null;
        for (E e : c)
            if (result == null || e.compareTo(result) > 0)
                result = Objects.requireNonNull(e);
        return result;
    }

}

The above method raises an IllegalArgumentException when the list is empty. A good alternative is to have a return value of ```Optional ` ``.

Recommended Posts

Item 30: Favar generic methods
Item 29: Favor generic types
Item 66: Use native methods judiciously
Item 88: Write readObject methods defensively