💚 Map.entrySet()
· 반환형: Set<Map. Entry<K, V>>
- Map 인터페이스의 메서드
- Map의 key-value 쌍을 Map.Entry 객체로 반환한다.
- 각 Map.Entry 객체는 key와 value를 포함하고 있으며, 이 객체들을 모아서 Set 인터페이스 형태로 반환한다.
- 주로 Map에 저장된 요소들을 반복하면서 key와 value를 함께 사용해야 하는 경우에 사용된다. 이를 통해 각 요소의 key와 value를 개별적으로 처리할 수 있다.
· Map.Entry 객체의 메서드
① getKey()
② getValue()
💚 사용 예시
import java.util.HashMap;
import java.util.Map;
public class MapEntryExample {
public static void main(String[] args) {
// HashMap 생성
Map<String, Integer> map = new HashMap<>();
map.put("A", 1);
map.put("B", 2);
map.put("C", 3);
// Map.Entry를 이용한 요소 반복
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String key = entry.getKey();
int value = entry.getValue();
System.out.println("Key: " + key + ", Value: " + value);
}
}
}
실행 결과
Key: A, Value: 1
Key: B, Value: 2
Key: C, Value: 3
참고 링크
https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#entrySet--
Map (Java Platform SE 8 )
If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null. If the function returns null no mapping is recorded. If the function
docs.oracle.com
https://docs.oracle.com/javase/8/docs/api/java/util/Map.Entry.html
Map.Entry (Java Platform SE 8 )
All Known Implementing Classes: AbstractMap.SimpleEntry, AbstractMap.SimpleImmutableEntry Enclosing interface: Map public static interface Map.Entry A map entry (key-value pair). The Map.entrySet method returns a collection-view of the map, whose elements
docs.oracle.com
'Programming > Java' 카테고리의 다른 글
[Java] 자바 비트 연산자(bitwise operator) (0) | 2023.08.08 |
---|---|
[Java] 자바 네이밍 규칙(Java Naming Convention) (0) | 2023.08.03 |
[Java] 자바 ArrayList와 LinkedList 비교 (0) | 2023.07.07 |
[Java] 자바 HashSet 정렬법 (0) | 2023.07.07 |
[Java] 자바 향상된 for문 (0) | 2023.06.30 |
댓글