카테고리 없음
chap12-08 HashMap<K,V>
푸곰주
2022. 3. 26. 00:36
package ex12;
import java.util.HashMap;
public class Ex12_22 {
public static void main(String[] args) {
HashMap<String, Student2>map = new HashMap<>();
//JDK1.7부터 생성자에 타입지정 생략가능.
map.put("자바왕", new Student2("자바왕", 1, 1, 100, 100, 100));
System.out.println(map);
//{자바왕=ex12.Student2@49e4cb85}
//key , 뒤에것이 value <-toString 오버라이딩 안해서 이렇게 나타난것;
//map에서 꺼낼대
Student2 s = map.get("자바왕");
//원래 (Student) map.get(); 형변환필요했으나 지네릭클래스,타입정보주었기때문에
// public V get(Object key) { V가 Student2로..반환타입이 Studnet2로
System.out.println(s.name);
System.out.println(map.get("자바왕").name);
//위와 아래는 같은코드
}
}
class Student2{
String name = "";
int ban;
int no;
int kor;
int eng;
int math;
Student2(String name, int ban, int no, int kor, int eng, int math){
this.name = name;
this.ban = ban;
this.no = no;
this.eng = eng;
this.math = math;
}
}