Computer/Tips
[Java] Map/List log 출력
창천(蒼天)
2021. 3. 22. 13:23
출처 : Java Map 전체 출력 (tistory.com)
[Java] Map / List 출력해보기 (tistory.com)
[Java] Map / List 출력해보기
종종 Map과 List에 있는 데이터를 보고 싶을때가 있다. 물론 debug 모드로 확인 가능하지만 log 출력하여 보고싶을때 사용하자 1. Map public static void printMap(Map map){ Iterator > iterator = map.entrySet..
cakas.tistory.com
Java Map 전체 출력
Map에 저장된 내용을 모두 출력하는 방식입니다. 3가지 타입이며 아래 예제를 확인하시길 바랍니다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38..
huskdoll.tistory.com
1. Map
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class MapIterationSample {
public static void main(String[] args) {
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("test3", "name5");
hashMap.put("test4", "name2");
hashMap.put("test5", "name1");
hashMap.put("test1", "name3");
hashMap.put("test2", "name4");
System.out.println("=================Type1=================");
Iterator<String> mapIter = hashMap.keySet().iterator();
while(mapIter.hasNext()){
String key = mapIter.next();
String value = hashMap.get( key );
System.out.println(key+" : "+value);
}
System.out.println("=================Type2=================");
for(Map.Entry<String, String> elem : hashMap.entrySet()){
String key = elem.getKey();
String value = elem.getValue();
System.out.println(key+" : "+value);
}
System.out.println("=================Type2=================");
for(String key : hashMap.keySet()){
String value = hashMap.get(key);
System.out.println(key+" : "+value);
}
}
}
2. List
public static void printList(List<Map<String,Object>> list){
Iterator<Entry<String,Object>> iterator = null;
Entry<String,Object> entry = null;
log.debug("--------------------printList--------------------\n");
int listSize = list.size();
for(int i=0; i<listSize; i++){
log.debug("list index : "+i);
iterator = list.get(i).entrySet().iterator();
while(iterator.hasNext()){
entry = iterator.next();
log.debug("key : "+entry.getKey()+",\tvalue : "+entry.getValue());
}
log.debug("\n");
}
log.debug("------------------------------------------------\n");
}