μ΄μμ΄λ©΄ μ λ€λ¦ λ©μλλ‘ λ§λ€λΌ
λ©μλλ μ λ€λ¦μΌλ‘ λ§λ€ μ μλλ°, μ΄λ₯Ό μ λ€λ¦ λ©μλλΌκ³ νλ©°, Collections
μ sort
λ©μλκ° λνμ μΈ μμ΄λ€.
λͺ
λͺ
κ·μΉμ΄λ μ¬μ© λ°©λ²μ μ λ€λ¦ ν΄λμ€μ λμΌνλ€.
class Test {
public static <E> Set<E> union(Set<E> s1, Set<E> s2) {
Set<E> result = new HashSet<>(s1);
result.addAll(s2);
return result;
}
public static void main(String[] args) {
Set<String> a = Set.of("a", "b", "c");
Set<String> b = Set.of("d", "e", "f");
Set<String> c = union(a, b);
}
}
λ§μ°¬κ°μ§λ‘ νμ μ μμΌλμΉ΄λ νμ
μΌλ‘ λ μ μ°νκ² λ§λ€ μ μλ€.
μ λ€λ¦ μ±κΈν΄ ν©ν°λ¦¬
λΆλ³ κ°μ²΄λ₯Ό μ¬λ¬ νμ
μΌλ‘ νμ©ν μ μκ² λ§λ€μ΄μΌ ν λκ° μλ€.
μ λ€λ μ λ°νμμ νμ
μ λ³΄κ° μκ±°λκΈ° λλ¬Έμ νλμ κ°μ²΄λ₯Ό μ΄λ€ νμ
μΌλ‘ 맀κ°λ³μν ν μ μλλ°, μ΄λ₯Ό μν΄μ νμ
μ λ°κΏμ£Όλ μ μ ν©ν°λ¦¬λ₯Ό λ§λ€μ΄μΌ νλ€.
μ μ ν©ν°λ¦¬ λ©μλμμλ μμ²ν νμ
맀κ°λ³μμ λ§κ² κ·Έ κ°μ²΄μ νμ
μ λ°κΏμ£Όλ μν μ μννκ² λλ©°, μ΄ ν¨ν΄μ μ λ€λ¦ μ±κΈν΄ ν©ν°λ¦¬λΌκ³ νλ€.
// Collectionsμμ μ¬μ©λ μ μ ν©ν°λ¦¬ λ©μλ reverseOrder
public class Collections {
// Suppresses default constructor, ensuring non-instantiability.
private Collections() {
}
// ...
@SuppressWarnings("unchecked")
public static <T> Comparator<T> reverseOrder() {
return (Comparator<T>) ReverseComparator.REVERSE_ORDER;
}
// ...
}
μ λ€λ¦ μ±κΈν΄ ν©ν°λ¦¬λ₯Ό νλ±ν¨μλ₯Ό λ΄μ ν΄λμ€μ λ©μλμ μ μ©νλ©΄ μλμ κ°μ΄ μ¬μ©ν μ μλ€.
class GenericSingletonFactory {
private static final UnaryOperator<Object> IDENTITY_FN = (t) -> t;
@SuppressWarnings("unchecked") // μμ μμ΄ κ·Έλλ‘ λ°ννλ―λ‘ μμ (=νμ
μμ μ± λ³΄μ₯)
public static <T> UnaryOperator<T> identityFunction() {
return (UnaryOperator<T>) IDENTITY_FN; // UnaryOperator<Object> != UnaryOperator<T> μΌλ‘ κ²½κ³ λ°μ
}
}
class Main {
public static void main(String[] args) {
String[] strings = {"μ€λ¦¬", "λꡬ리", "μ€κ΅¬"};
// Stringμ μΈμλ‘ λ°μ Stringμ λ°ννλ UnaryOperator
UnaryOperator<String> sameString = GenericSingletonFactory.identityFunction();
for (String s : strings) System.out.println(sameString.apply(s));
Number[] numbers = {5, 9.0, 59L};
// Numberλ₯Ό μΈμλ‘ λ°μ Numberλ₯Ό λ°ννλ UnaryOperator
UnaryOperator<Number> sameNumber = GenericSingletonFactory.identityFunction();
for (Number n : numbers) System.out.println(sameNumber.apply(n));
}
}
μ¬κ·μ νμ
νμ (recursive type bound)
μκΈ° μμ μ΄ λ€μ΄κ° ννμμ μ¬μ©ν΄ νμ
맀κ°λ³μμ νμ© λ²μλ₯Ό νμ νλ μ¬κ·μ νμ
νμ μ΄λΌλ κ°λ
λ μ‘΄μ¬νλ€.
μ¬κ·μ νμ
νμ μ μ£Όλ‘ Comparable
μΈν°νμ΄μ€μ ν¨κ» μ¬μ©νλ€.
// Comparable μΈν°νμ΄μ€
public interface Comparable<T> {
int compareTo(T o);
}
// java.util.Collectionsμ max λ©μλ
public class Collections {
// Suppresses default constructor, ensuring non-instantiability.
private Collections() {
}
// ...
public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll) {
Iterator<? extends T> i = coll.iterator();
T candidate = i.next();
while (i.hasNext()) {
T next = i.next();
if (next.compareTo(candidate) > 0)
candidate = next;
}
return candidate;
}
// ...
}
κ°μ μνΈ λΉκ΅νκΈ° μν΄μλ 컬λ μ
μ λ΄κΈ΄ λͺ¨λ μμκ° μνΈ λΉκ΅λ μ μμ΄μΌ νλ€.
Collection
μ max
λ©μλλ μ μμ μΌλ‘ κΈ°λ₯μ μννκΈ° μν΄μ μνΈ λΉκ΅κ° λμΌνλ―λ‘ T
κ° Comparable
μ ꡬννλλ‘ μ ννκ³ μλ€.