주제

 

Map 사용하기

 

Interface Map<Object, Object>

 

함수

put(key, value)

: 데이터를 넣을때 사용함

get(key)

: 데이터를 가져올때 사용함

 

Class HashMap<Object, Object>

 

 

 사용법

 

여기서는 key, value를 모두 String로 사용한다.

        // Map
        Map<String, String> map = new HashMap<String, String>();
        map.put("spring", "봄");
        map.put("summer", "여름");
        map.put("autumn", "가을");
        map.put("winter", "겨울");
        
        System.out.println("spring: " + map.get("spring"));
        System.out.println("summer: " + map.get("summer"));
        System.out.println("autumn: " + map.get("autumn"));
        System.out.println("winter: " + map.get("winter"));
        
        /*
        // 출력 결과
        spring: 봄
        summer: 여름
        autumn: 가을
        winter: 겨울
        */

 

 

 출처

 

Oracle Java

 

Interface Map<K,V>

Type Parameters:
K - the type of keys maintained by this map
V - the type of mapped values

Modifier and Type Method and Description
V get(Object key)
Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
V put(K key, V value)
Associates the specified value with the specified key in this map (optional operation).

https://docs.oracle.com/javase/8/docs/api/java/util/Map.html

 

Class HashMap<K,V>

Type Parameters:
K - the type of keys maintained by this map
V - the type of mapped values

https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html

 

 

728x90

+ Recent posts