프로그래밍/- java
지네릭스<E> , iterator<E> , HashMap<k,v>
즐겁게 하하하
2022. 2. 4. 09:08
728x90

★ 컴파일시 타입을 체크해 주는 기능
- static 맴버에 타입변수 사용 불가
- 배열, 객체 생성할때 타입변수T 사용불가 new T[1] (x)
ArrayList<Tv> tvList = new ArrayList<Tv>(); :: Tv타입만 넣을수 있다.
tvList.add( new Tv() ); :: 자신 add ok
tvList.add( new LgTv() ); :: 자손 add ok
tvList.add( new Audio() ) :: Error
Tv i = list.get(0);
LgTv i = (LgTv)list.get(0);
ArrayList<Integer> tvList = new ArrayList<Integer>(); :: 컴파일 에러를 방지해 준다.
tvList.add( 1 );
tvList.add( 2 );
Integer i = (Integer)list.get(1); :: .get() => Object 타입 반환 / (Integer) 생략 가능
ArrayList<Object> tvList = new ArrayList<Object>();
String i = (String)list.get(1);
★ 제한된 지네릭 클래스
class FruitBox< T extends Fruit >{ } :: Fruit 자손만 타입으로 지정 가능
class FruitBox< T extends InterFace1 & InterFace2 >{ } :: 인터페이스도 extends 사용
★ 와일드 카드 <?> :: 하나의 참조 변수로 대입된 타입이 다른 객체를 참조 가능
ArrayList<TV> tvList = new ArrayList<TV>() :: 기존
ArrayList<TV> tvList = new ArrayList<LgTv>() :: 타입 불일치 Error
ArrayList<? extends TV> list = new ArrayList<LgTv>() :: 와일드카드
<? extends T> :: T의 자손들만 가능
<? super T> :: T의 조상들만 가능
<?> :: 제한없음
static Juice makeJuice( FruitBox<? extends Fruit> box ) { } :: 메서드 매개변수 ok
★ 지네릭 메서드 ( 지네릭 타입이 선언된 메서드 )
- 타입 변수는 메서드 내에서만 유효
static <T extends Fruit> Juice makeJuice( FruitBox<T> box ){ }
FruitBox<Fruit> fruitBox = new FruitBox<Fruit>();
System.out.println( Juicer.<Fruit>makeJuice(fruitBox) );
★ 지네릭 형변환
Box<Object> objBox = new Box<Object>();
Box<String> strBox = new Box<String>();
- Box<Object> objBox = (Box<Object>)strBox; :: 불가능 , Box<Object> -> Box<String>
- Box<? extends Object> wBox = new Box<String>(); :: 형변환 가능
★ 컴파일러는 지네릭 타입을 제거하고 필요한 곳에 형변환을 넣는다.
import java.util.ArrayList;
class Fruit2 { public String toString() { return "Fruit";}}
class Apple2 extends Fruit2 { public String toString() { return "Apple";}}
class Grape2 extends Fruit2 { public String toString() { return "Grape";}}
class Juice {
String name;
Juice(String name) { this.name = name + "Juice"; }
public String toString() { return name; }
}
class Juicer {
static Juice makeJuice(FruitBox2<? extends Fruit2> box) {
String tmp = "";
for(Fruit2 f : box.getList())
tmp += f + " ";
return new Juice(tmp);
}
}
class Ex12_4 {
public static void main(String[] args) {
FruitBox2<Fruit2> fruitBox = new FruitBox2<Fruit2>();
FruitBox2<Apple2> appleBox = new FruitBox2<Apple2>();
fruitBox.add(new Apple2());
fruitBox.add(new Grape2());
appleBox.add(new Apple2());
appleBox.add(new Apple2());
System.out.println(Juicer.makeJuice(fruitBox));
System.out.println(Juicer.makeJuice(appleBox));
} // main
}
class FruitBox2<T extends Fruit2> extends Box2<T> {}
class Box2<T> {
ArrayList<T> list = new ArrayList<T>();
void add(T item) { list.add(item); }
T get(int i) { return list.get(i); }
ArrayList<T> getList() { return list; }
int size() { return list.size(); }
public String toString() { return list.toString();}
}
★ ArrayList
ArrayList<Student> list = new ArrayList<Student>();
list.add(new Student("자바왕", 1, 1));
list.add(new Student("자바짱", 1, 2));
list.add(new Student("홍길동", 2, 1));
★ Iterator<E>
Iterator<Student> it = list.iterator();
while (it.hasNext()) {
//Student s = (Student)it.next(); // 지네릭스를 사용하지 않으면 형변환 필요.
//Student s = it.next();
//System.out.println(s.name);
System.out.println(it.next().name);
}
★ HashMap<k,v>
HashMap<String , Student> map = new HashMap<>();
map.put(" 자바왕 " , new Student() );
728x90