개발자료/Android
HashMap 전체 참조(foreach) 방법 (Java)
이것저것Root
2020. 7. 21. 23:51
반응형
HashMap 에 포함된 Key, Value 값을 모두 확인하는 방법
# 데이터 생성
HashMap<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
# 방법 1
Iterator<String> keys = map.keySet().iterator();
while( keys.hasNext() ){
String strKey = keys.next();
String strValue = map.get(strKey);
System.out.println( strKey +":"+ strValue );
}
# 방법 2
for( Map.Entry<String, String> entry : map.entrySet() ){
String strKey = entry.getKey();
String strValue = entry.getValue();
System.out.println( strKey +":"+ strValue );
}
# 방법 3
for( String strKey : map.keySet() ){
String strValue = map.get(strKey);
System.out.println( strKey +":"+ strValue );
}
# 방법 4
// Java 1.8 이상
map.forEach((strKey, strValue)->{
System.out.println( strKey +":"+ strValue );
});
반응형