
public class GenericTest<T> {
private T value;
public T getValue() { return value; }
public void setValue(T value) { this.value = value; }
}
// 사용
GenericTest<Integer> gt = new GenericTest<>();
gt.setValue(10);
<?> : 모든 타입 허용<? extends T> : T 또는 하위 타입만<? super T> : T 또는 상위 타입만public void anyType(RabbitFarm<?> farm) { farm.getAnimal().cry(); }
public void extendsType(RabbitFarm<? extends Bunny> farm) { farm.getAnimal().cry(); }
public void superType(RabbitFarm<? super Bunny> farm) { farm.getAnimal().cry(); }
기억법: PECS — Producer-Extends, Consumer-Super
읽기(생산자)는extends, 쓰기(소비자)는super가 안전.
Collection 하위: List, Set, Queue| 인터페이스 | 특징 | 대표 구현체 |
|---|---|---|
| List\ | 순서 O, 중복 O | ArrayList, LinkedList, Vector, Stack |
| Set\ | 순서 X, 중복 X | HashSet, LinkedHashSet, TreeSet |
| Map\<K,V> | 키-값, 키 중복 X | HashMap, LinkedHashMap, TreeMap |
List<Object> alist = new ArrayList<>();
alist.add("apple"); alist.add(123); alist.add(45.67);
System.out.println(alist); // toString 오버라이드됨
System.out.println(alist.size()); // 요소 개수
System.out.println(alist.get(0)); // 인덱스 접근
alist.add(1, "banana"); // 중간 삽입(뒤로 밀림)
alist.set(1, true); // 수정
alist.remove(2); // 삭제
List<String> list = new ArrayList<>();
list.add("banana"); list.add("orange"); list.add("apple");
Collections.sort(list); // 오름차순
// 내림차순(람다)
list.sort((a,b) -> b.compareTo(a));
class BookDTO {
int number, price; String title, author;
// 생성자/getter/toString 생략
}
// 가격 오름차순
list.sort(Comparator.comparingInt(BookDTO::getPrice));
// 가격 내림차순
list.sort(Comparator.comparingInt(BookDTO::getPrice).reversed());
// 제목 오름차순
list.sort(Comparator.comparing(BookDTO::getTitle));
List<String> linked = new LinkedList<>();
linked.add("apple"); linked.add("banana");
linked.remove(0); // 링크만 수정 → 삭제 비용 낮음
linked.set(0, "fineapple");
System.out.println(linked.isEmpty());
linked.clear();
push, peek(조회), pop(꺼내며 삭제), searchStack<Integer> st = new Stack<>();
st.push(1); st.push(2); st.push(3);
System.out.println(st.peek()); // 3
System.out.println(st.pop()); // 3 → 제거
System.out.println(st); // [1, 2]
빈 스택에서
pop/peek→EmptyStackException
offer(삽입), peek(조회), poll(꺼내며 삭제)LinkedList 사용Queue<String> q = new LinkedList<>();
q.offer("first"); q.offer("second");
System.out.println(q.peek()); // first
System.out.println(q.poll()); // first → 제거
System.out.println(q); // [second]
null은 1개만Set<String> set = new HashSet<>();
set.add("java"); set.add("oracle"); set.add("java"); // 중복 무시
System.out.println(set.contains("oracle"));
for (String s : set) System.out.println(s);
set.clear();
Set<String> lhs = new LinkedHashSet<>(List.of("java","oracle","jdbc"));
System.out.println(lhs); // [java, oracle, jdbc]
정렬 필요 시 →
new TreeSet<>(lhs)로 변환
TreeSet<String> tset = new TreeSet<>();
tset.add("java"); tset.add("oracle"); tset.add("css");
System.out.println(tset); // [css, java, oracle]
// 로또(중복 제거 + 정렬)
Set<Integer> lotto = new TreeSet<>();
while (lotto.size() < 6) lotto.add((int)(Math.random()*45)+1);
System.out.println(lotto); // 예: [6, 8, 13, 22, 28, 36]
ArrayList(조회↑), LinkedList(삽입/삭제↑)HashSet(빠름), LinkedHashSet(삽입순서 유지), TreeSet(정렬)Stack/Queue(LinkedList)Collections.sort or List.sort(Comparator)