스텝 1 - 제네릭의 필요성
package com.in28minutes.generics;
public class GenericsRunner {
public static void main(String[] args) {
MycustomList list = new MycustomList();
list.addElement("Element 1");
list.addElement("Element 2");
MycustomList list2 = new MycustomList();
list.addElement(Integer.valueOf(5));
list.addElement(Integer.valueOf(6));
}
}
package com.in28minutes.generics;
import java.util.ArrayList;
public class MycustomList {
ArrayList<String> list = new ArrayList();
public void addElement(String element) {
list.add(element);
}
public void removeElement(String element) {
list.remove(element);
}
}
스텝 2 - 제네릭 구현
package com.in28minutes.generics;
public class GenericsRunner {
public static void main(String[] args) {
MycustomList<String> list = new MycustomList<>();
list.addElement("Element 1");
list.addElement("Element 2");
System.out.println(list);
MycustomList<Integer> list2 = new MycustomList<>();
list2.addElement(Integer.valueOf(5));
list2.addElement(Integer.valueOf(6));
System.out.println(list2);
}
}
package com.in28minutes.generics;
import java.util.ArrayList;
public class MycustomList<T> {
ArrayList<T> list = new ArrayList();
public void addElement(T element) {
list.add(element);
}
public void removeElement(T element) {
list.remove(element);
}
@Override
public String toString() {
return list.toString();
}
}
스텝 3 - 제네릭 return 메소드
package com.in28minutes.generics;
public class GenericsRunner {
public static void main(String[] args) {
MycustomList<String> list = new MycustomList<>();
list.addElement("Element 1");
list.addElement("Element 2");
String value = list.get(0);
System.out.println(value);
MycustomList<Integer> list2 = new MycustomList<>();
list2.addElement(Integer.valueOf(5));
list2.addElement(Integer.valueOf(6));
Integer number = list2.get(0);
System.out.println(number);
}
}
package com.in28minutes.generics;
import java.util.ArrayList;
public class MycustomList<T> {
ArrayList<T> list = new ArrayList();
public void addElement(T element) {
list.add(element);
}
public void removeElement(T element) {
list.remove(element);
}
@Override
public String toString() {
return list.toString();
}
public T get(int index) {
return list.get(index);
}
}
스텝 4 - 제한과 확장, 제네릭 메소드
package com.in28minutes.generics;
import java.util.ArrayList;
public class GenericsRunner {
static <X> X doubleValue(X value){
return value;
}
static <X extends List> void duplicate(X list){
list.addAll(list);
}
public static void main(String[] args) {
String value = doubleValue(new String());
Integer number = doubleValue(Integer.valueOf(5));
ArrayList list1 = doubleValue(new ArrayList());
ArrayList<Integer> numbers = new ArrayList<>(List.of(1,2,3));
duplicate(numbers);
System.out.println(numbers);
MycustomList<String> list = new MycustomList<>();
list.addElement("Element 1");
list.addElement("Element 2");
String value = list.get(0);
System.out.println(value);
MycustomList<Integer> list2 = new MycustomList<>();
list2.addElement(Integer.valueOf(5));
list2.addElement(Integer.valueOf(6));
Integer number = list2.get(0);
System.out.println(number);
}
}
package com.in28minutes.generics;
import java.util.ArrayList;
public class MycustomList<T extends Number> {
ArrayList<T> list = new ArrayList();
public void addElement(T element) {
list.add(element);
}
public void removeElement(T element) {
list.remove(element);
}
@Override
public String toString() {
return list.toString();
}
public T get(int index) {
return list.get(index);
}
}
스텝 5 - 제네릭과 와일드 카드
package com.in28minutes.generics;
import java.util.ArrayList;
public class GenericsRunner {
static <X> X doubleValue(X value){
return value;
}
static <X extends List> void duplicate(X list){
list.addAll(list);
}
static double sumOfNumberList(list<? extends Number> numbers){
double sum = 0.0;
for(Number number : numbers){
sum += number.doubleValue();
}
return sum;
}
static void addACoupleValues(list<? super Number> numbers){
numbers.add(1);
numbers.add(1.0);
numbers.add(1.0f);
numbers.add(1l);
}
public static void main(String[] args) {
List emptyList = new ArrayLisy<number>();
addACoupleValues(emptyList);
System.out.println(emptyList);
System.out.println(sumOfNumberList(List.of(1,2,3,4,5)));
System.out.println(sumOfNumberList(List.of(1.1,2.1,3.1,4.1,5.1)));
System.out.println(sumOfNumberList(List.of(1l,2l,3l,4l,5l)));
String value = doubleValue(new String());
Integer number = doubleValue(Integer.valueOf(5));
ArrayList list1 = doubleValue(new ArrayList());
ArrayList<Integer> numbers = new ArrayList<>(List.of(1,2,3));
duplicate(numbers);
System.out.println(numbers);
MycustomList<String> list = new MycustomList<>();
list.addElement("Element 1");
list.addElement("Element 2");
String value = list.get(0);
System.out.println(value);
MycustomList<Integer> list2 = new MycustomList<>();
list2.addElement(Integer.valueOf(5));
list2.addElement(Integer.valueOf(6));
Integer number = list2.get(0);
System.out.println(number);
}
}
package com.in28minutes.generics;
import java.util.ArrayList;
public class MycustomList<T extends Number> {
ArrayList<T> list = new ArrayList();
public void addElement(T element) {
list.add(element);
}
public void removeElement(T element) {
list.remove(element);
}
@Override
public String toString() {
return list.toString();
}
public T get(int index) {
return list.get(index);
}
}