HashMap

stan·2022년 11월 14일
0

Java 개념

목록 보기
2/33
package collection;

import java.util.HashMap;
import java.util.Map;

public class Main01 {

	public static void main(String[] args) {
		//제너릭 -> <값의 이름, 값의 종류>
		Map<String, Integer> hm = new HashMap<>();
		//Map<String, Integer> hm2 = new HashMap<String, Integer>();
		
		// 데이터 추가는 put 메소드 사용
		// -> 키값을 중심으로 중복을 허용하지 않는다. 
		hm.put("국어", 80);
		hm.put("수학", 60);
		hm.put("영어", 70);
		hm.put("프로그래밍", null); // 객체를 넣는 것이므로 null사용가능, 프로그래밍 시험을 보지 않았다. 
		
		// 저장된 갯수 얻기 
		System.out.println("HashMap Size : " + hm.size());
		
		// 데이터 꺼내기
		System.out.println( hm.get("국어") );
		System.out.println( hm.get("영어") );
		System.out.println( hm.get("수학") );
		System.out.println( hm.get("프로그래밍") );
		
		System.out.println("----------------------------------");
		hm.put("프로그래밍", 100);
	
		System.out.println( hm.get("국어") );
		System.out.println( hm.get("영어") );
		System.out.println( hm.get("수학") );
		System.out.println( hm.get("프로그래밍") );
		
		System.out.println("----------------------------------");
		
		System.out.println( hm.containsKey("국어"));
		System.out.println( hm.containsKey("체육"));
		
		
		
		
		
	}

}

키 값에는 클라스만 넣을 수 있음

Wrapper Class Integer 로 숫자 저장

A HashMap however, store items in "key/value" pairs, and you can access them by an index of another type (e.g. a String).

One object is used as a key (index) to another object (value). It can store different types: String keys and Integer values, or the same type, like: String keys and String values:

profile
이진 입니다

0개의 댓글

관련 채용 정보