// 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;
}
identity_fn
Tounaryoperator<t>
I get a warning when I cast toidentity
関数は引数To変更せずに返却するので、warningTo抑制することができる。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;
}
}
Comparable
interface.public interface Comparable<T> {
int compareTo(T o);
}
`Collection`
rearrangement, sorting, and maximum / minimum derivation methods use elements that implement `` `Comparable```. These methods require elements that can be compared with each other, which are expressed as follows.// 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