샘플(1)

아래와 같이 2개의 데이터를 저장해보도록 하겠습니다.


Map map;
List list = new ArrayList();

// 첫번째
map = new HashMap();
map.put("국어", 90);
map.put("수학", 80);
list.add(map);

// 두번째
map = new HashMap();
map.put("국어", 55);
map.put("수학", 65);
list.add(map);


System.out.println(list);

[{수학=80, 국어=90}, {수학=65, 국어=55}]



샘플(2)


이제 집어넣은 데이터를 가져올 차례입니다.

 

List 에 저장된 HashMap 데이터를 사용하기 위에서는 HashMap을 만든 다음, 캐스팅 연산으로 가져오도록 합니다.


HashMap getMap = new HashMap();

getMap = (HashMap)list.get(0);
System.out.println("1번째 국어 점수 : " + getMap.get("국어"));

getMap = (HashMap)list.get(1);
System.out.println("2번째 국어 점수 : " + getMap.get("국어"));



1번째 국어 점수 : 90

2번째 국어 점수 : 55

+ Recent posts