π‘ ν΄λμ€ λ΄λΆμμ μ¬μ©ν λ°μ΄ν° νμ μ μΈλΆμμ μ§μ νλ κΈ°λ², μ¦ νμ μ λ³μν
ArrayList<String> list = new ArrayList<>();
< > κ° μ λ€λ¦<T>ꡬ체ν: μΈμ€ν΄μ€ μμ± μ νμ μ§μ β μ λ€λ¦ νμ μ ν
νμ νλΌλ―Έν° μλ΅ κ°λ₯ (μμ±μ νμ μΆλ‘ )
Reference νμ λ§ κ°λ₯ (μμ νμ X)
Java μ λ€λ¦μ μ»΄νμΌ νμ νμ
μκ±°(Type Erasure) κΈ°λ°
primitive μ¬μ© λΆκ° β Wrapper(Integer λ±) μλ λ°μ±μΌλ‘ μ€λ¬΄ λΆνΈ κ±°μ μμ
μ λ€λ¦ νμ λ³μ κ΄λ‘
T item = new T(); // λΆκ°
T[] arr = new T[10]; // λΆκ°
class FruitBox<T> {
List<T> fruits = new ArrayList<>();
public void add(T fruit) {
fruits.add(fruit);
}
}
FruitBox<Integer> intBox = new FruitBox<>();
FruitBox<Double> doubleBox = new FruitBox<>();
FruitBox<String> strBox = new FruitBox<>();
FruitBox<Apple> appleBox = new FruitBox<>();
class Apple {}
class Banana {}
class FruitBox<T, U> {
List<T> apples = new ArrayList<>();
List<U> bananas = new ArrayList<>();
public void add(T apple, U banana) {
apples.add(apple);
bananas.add(banana);
}
}
ArrayList<LinkedList<String>> list = new ArrayList<>();
LinkedList<String> node1 = new LinkedList<>();
node1.add("aa"); node1.add("bb");
interface ISample<T> {
void addElement(T t, int index);
T getElement(int index);
}
interface IAdd<T> { T add(T x, T y); }
IAdd<Integer> sum = (x, y) -> x + y;
class Box<T> {
public <U> void print(U value) { System.out.println(value); }
}
// μ€μ μ¬μ©
class FruitBox<T> {
// ν΄λμ€μ νμ
νλΌλ―Έν°λ₯Ό λ°μμ μ¬μ©νλ μΌλ° λ©μλ
public T addBox(T x, T y) {
// ...
}
// λ
립μ μΌλ‘ νμ
ν λΉ μ΄μλλ μ λ€λ¦ λ©μλ
public static <T> T addBoxStatic(T x, T y) {
// ...
}
}
FruitBox.<Integer>addBoxStatic(1, 2);
FruitBox.<String>addBoxStatic("μλ
", "μκ°");
// λ©μλμ μ λ€λ¦ νμ
μλ΅
FruitBox.addBoxStatic(1, 2);
FruitBox.addBoxStatic("μλ
", "μκ°");
class A {}
interface B {}
class Sample<T extends A & B> {}
class Box<T extends Comparable<T>> {}
μ λ€λ¦ νλ³ν
π‘λ³μ±(Variance): νμ κ° μμ κ΄κ³κ° μ λ€λ¦μμ μ΄λ»κ² λ°μλλκ°
곡λ³μ±: μλ‘ λ€λ₯Έ νμ κ°μ ν¨κ³ λ³ν μ μλ€λ νΉμ§(Liskov μΉν μμΉ)
곡λ³Β :Β SΒ κ°Β TΒ μ νμ νμ μ΄λ©΄, S[] λ T[] μ νμ νμ μ΄λ€. List λ List μ νμ νμ μ΄λ€.
λ°κ³΅λ³Β :Β SΒ κ°Β Tμ νμ ν μ€νΈνμ μ΄λ©΄, T[] λ S[] μ νμ νμ μ΄λ€. (곡λ³μ λ°λ). ListΒ λ List μ νμ νμ μ΄λ€. (곡λ³μ λ°λ)
λ¬΄κ³΅λ³ / λΆκ³΅λ³Β :Β SΒ μΒ TΒ λ μλ‘ κ΄κ³κ° μλ€. List μ List λ μλ‘ λ€λ₯Έ νμ μ΄λ€.
// 곡λ³μ±
Object[] Covariance = new Integer[10];
// λ°κ³΅λ³μ±
Integer[] Contravariance = (Integer[]) Covariance;
// 곡λ³μ±
ArrayList<Object> Covariance = new ArrayList<Integer>();
// λ°κ³΅λ³μ±
ArrayList<Integer> Contravariance = new ArrayList<Object>();
μ λ€λ¦ νμ μ λν΄μ 곡λ³μ± / λ°κ³΅λ³μ±μ μ§μνμ§ μμ
Object[] numbers = new Integer[10];
List<Object> list = new ArrayList<Integer>(); // μ»΄νμΌ μλ¬
numbers[0] = "text"; // λ°νμ μλ¬
List<Integer> ints = Arrays.asList(1,2,3);
List<? extends Number> nums = ints;
<?> (Unbounded)<? extends T> (μν μ ν)<? super T> (νν μ ν)List<out T>
List<in T>
Producer Extends, Consumer Super
void printAll(List<? extends Number> list) {
for (Number n : list) System.out.println(n);
}
void addNumbers(List<? super Integer> list) {
list.add(10);
list.add(20);
}
μμΌλμΉ΄λλ μ€κ³κ° μλ μ¬μ©μ μν κ²
T extends νμ , ? extends U μ°¨μ΄μ
T super νμ : 무μν λ§μ μλ°μ ν΄λμ€μ μΈν°νμ΄μ€ μ¬ μ μκΈ°λλ¬Έμ μ¬μ© x
? μ object μ°¨μ΄: null λ§ λ£μ μ μμ <-> object νμ νμ
λͺ¨λ λ£μ μ μμ
| νμ
| λ£κΈ°(set) | μ½κΈ°(get) | μ¬μ© λͺ©μ |
| -------------- | ----------- | --------------- | ---------------- |
| List<?> | nullλ§ κ°λ₯ | Object νμ
μΌλ‘ GET | μ½κΈ° μ μ©, νμ₯μ± |
| List<Object> | λͺ¨λ νμ
λ£κΈ° κ°λ₯ | Objectλ‘ GET | μ€μ Object νμ
컬λ μ
|
π μ°Έκ³