[JAVA] Map.Entry와 entrySet()

qwe8851·2022년 11월 15일
0

🤮 JAVA

목록 보기
1/1
package Test;

import java.util.*;
import java.util.Map.Entry;


public class test{

    public static void main(String[] args) {

        
        Map<String, String> hashmap = new HashMap();
        hashmap.put("1", "프랑스");
        hashmap.put("2", "한국");
        hashmap.put("3", "미국");
        hashmap.put("4", "아랍에미리트");
        hashmap.put("5", "일본");

        System.out.println();
        System.out.println("변경전");
        for (Map.Entry m : hashmap.entrySet()) {
            System.out.printf("[번호 : %s\t국가 : %s]\n", m.getKey(), m.getValue());
        }

        // Value가 '일본'인 entry의 Value를 '중국'으로 변경
        for (Map.Entry m : hashmap.entrySet()) {
            if (m.getValue().equals("일본")) {
                m.setValue("중국");
            }
        }

        System.out.println();
        System.out.println("변경후");
        for (Map.Entry m : hashmap.entrySet()) {
            System.out.printf("[번호 : %s\t국가 : %s]\n", m.getKey(), m.getValue());
        }

        
        /*
        변경전
        [번호 : 1 국가 : 프랑스]
        [번호 : 2 국가 : 한국]
        [번호 : 3 국가 : 미국]
        [번호 : 4 국가 : 아랍에미리트]
        [번호 : 5 국가 : 일본]
        
        변경후
        [번호 : 1 국가 : 프랑스]
        [번호 : 2 국가 : 한국]
        [번호 : 3 국가 : 미국]
        [번호 : 4 국가 : 아랍에미리트]
        [번호 : 5 국가 : 중국]    <-- entrySet()의 return 값에 대한 변경으로 인해
                                        entrySet() 재실행 시의 값이 변한 것을 확인할 수 있다
        */
    
        } // end-main
    } // end-class
        
        /*
         * Map.Entry
         * 
         * Map.Entry는 Map의 nested class(static) 이다.
         * key와 value로 하나의 쌍을 이루는 map entry이다.
         * Map.Entry() 함수는 map의 collection-view를 return 한다.
         * Map.Entry()가 return하는 map entry에 참조값을 담기 위해서는
         * 반드시 타겟 컬렉션의 iterator에서 꺼내야만 한다.
         * 
         * 
         * ~.entrySet()
         * 
         * 해당 map에 담겨있는 key와 value의 연결들(mappings)을 반환한다. (a Set of the mappings를 반환)
         * 반환되는 set은 map에 의해 정해지는데, 반대로 map 역시 이 entrySet에 변화가 있으면 이 변화가 적용된다.
         * 
         *
         *  정리>>
         *  Map.Entry interface type으로 선언된 m 변수가 hashmap.entrySet()으로 return되는
         *  a Set of the mappings들을 하나씩 받아서 반복을 돌면서 key와 value에 따로 접근하며
         *  접근 후 값이 변화하면 map에서의 값도 변화하게 된다
         * (참고 : so changes to the map are reflected in the set, and vice-versa.)
         *  
         *  
         * Map.Entry< K , V > public static interface Map.Entry< K , V >
         * 
         * A map entry (key-value pair). The Map.entrySet method returns a
         * collection-view of the map, whose elements are of this class. The only way to
         * obtain a reference to a map entry is from the iterator of this
         * collection-view. These Map.Entry objects are valid only for the duration of
         * the iteration; more formally, the behavior of a map entry is undefined if the
         * backing map has been modified after the entry was returned by the iterator,
         * except through the setValue operation on the map entry.
         * 
         * 
         * entrySet public Set<Map.Entry< K , V >> entrySet()
         * 
         * Returns a Set view of the mappings contained in this map. The set is backed
         * by the map, so changes to the map are reflected in the set, and vice-versa.
         * If the map is modified while an iteration over the set is in progress (except
         * through the iterator's own remove operation, or through the setValue
         * operation on a map entry returned by the iterator) the results of the
         * iteration are undefined. The set supports element removal, which removes the
         * corresponding mapping from the map, via the Iterator.remove, Set.remove,
         * removeAll, retainAll and clear operations. It does not support the add or
         * addAll operations.
         * 
         * 
         */
         ```
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         https://surhommejk.tistory.com/217
profile
FrontEnd Developer with React, TypeScript

0개의 댓글