Language/Java

[Java] Map 모든 데이터 가져오기

byunghyun23 2022. 1. 21. 16:12

Java의 Map은 key, value로 구성되어있으며, Python의 dictionary와 같습니다.

get(key)를 사용하여 value를 가져올 수 있지만 key에 해당하는 value만 가져올 수 있습니다.

코드를 작성하다보면 Map 객체의 모든 데이터가 필요한 경우가 생기기 마련입니다.

 

본 포스팅은 Map의 모든 데이터를 가져오는 방법을 소개합니다.

흔히 3가지 방법이 존재합니다.

 

Map 객체에는 다음과 같이 데이터를 put() 하였습니다.

1. map.keyset().iterator()

keyset()을 이용하여 Set<String> 타입의 객체를 얻고 이것을 Iterator<String> 타입의 객체로 변환하여 데이터를 가져옵니다.

public String getMapDataWithIterator() {
	String result = "";

	Iterator<String> keys = map.keySet().iterator();
	while (keys.hasNext()) {
		String key = keys.next();
		String value = map.get(key);
		result += "key : " + key + ",    value : " + value + "\n";
	}

	return result;
}

 

2. map.entrySet()

entrySet()을 이용하여 Set<Entry<String, String>> 객체를 얻고 각 원소의 getKey(), getValue()를 통해 데이터를 가져옵니다.

public String getMapDataWithEntrySet() {
	String result = "";

	for (Map.Entry<String, String> element : map.entrySet()) {
		String key = element.getKey();
		String value = element.getValue();
        result += "key : " + key + ",    value : " + value + "\n";
	}
    
	return result;
}

 

3. map.keySet()

keyset()을 이용하여 Set 타입의 객체를 얻고(key를 얻고) get()를 통해 데이터를 가져옵니다.

public String getMapDataWithKeySet() {
	String result = "";
		
	for (String key : map.keySet()) {
		String value = map.get(key);
		result += "key : " + key + ",    value : " + value + "\n";
	}

	return result;
}

 

개인적으로 3번 방법이 간결하고 이해하기 쉬워 보입니다.

 

결과는 아래와 같습니다.

전체 코드입니다.

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

public class main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		MapManager mapManager = new MapManager();
		String result = null;

		result = mapManager.getMapDataWithIterator();
		System.out.println("getMapDataWithIterator");
		System.out.println(result);
		
		result = mapManager.getMapDataWithEntrySet();
		System.out.println("getMapDataWithEntrySet");
		System.out.println(result);
		
		result = mapManager.getMapDataWithKeySet();
		System.out.println("getMapDataWithKeySet");
		System.out.println(result);
	}
}

class MapManager {
	Map<String, String> map;

	public MapManager() {
		map = new HashMap<String, String>();

		map.put("age", "28");
		map.put("sex", "M");
	}

	public String getMapDataWithIterator() {
		String result = "";

		Iterator<String> keys = map.keySet().iterator();
		while (keys.hasNext()) {
			String key = keys.next();
			String value = map.get(key);
			result += "key : " + key + ",    value : " + value + "\n";
		}

		return result;
	}

	public String getMapDataWithEntrySet() {
		String result = "";

		for (Map.Entry<String, String> element : map.entrySet()) {
			String key = element.getKey();
			String value = element.getValue();
			result += "key : " + key + ",    value : " + value + "\n";
		}

		return result;
	}

	public String getMapDataWithKeySet() {
		String result = "";
		
		for (String key : map.keySet()) {
			String value = map.get(key);
			result += "key : " + key + ",    value : " + value + "\n";
		}

		return result;
	}
}