Java의 HashMap EntrySet() 메소드
Java의 java.util.HashMap.entrySet() 메소드는 해시 맵에 포함된 동일한 요소의 집합을 생성하는 데 사용됩니다. 기본적으로 해시 맵의 집합 보기를 반환하거나 새 집합을 만들고 여기에 맵 요소를 저장할 수 있습니다.
통사론:
hash_map.entrySet()
매개변수: 이 메서드는 매개변수를 사용하지 않습니다.
반환 값: 이 메서드는 해시 맵과 동일한 요소를 가진 집합을 반환합니다.
아래 프로그램은 java.util.HashMap.entrySet() 메소드의 작동을 설명하는 데 사용됩니다.
프로그램 1: 문자열 값을 정수 키에 매핑합니다.
// Java code to illustrate the entrySet() method> import> java.util.*;> > public> class> Hash_Map_Demo {> > public> static> void> main(String[] args)> > {> > > // Creating an empty HashMap> > HashMap hash_map => new> HashMap();> > > // Mapping string values to int keys> > hash_map.put(> 10> ,> 'Geeks'> );> > hash_map.put(> 15> ,> '4'> );> > hash_map.put(> 20> ,> 'Geeks'> );> > hash_map.put(> 25> ,> 'Welcomes'> );> > hash_map.put(> 30> ,> 'You'> );> > > // Displaying the HashMap> > System.out.println(> 'Initial Mappings are: '> + hash_map);> > > // Using entrySet() to get the set view> > System.out.println(> 'The set is: '> + hash_map.entrySet());> > }> }> |
산출:
Initial Mappings are: {20=Geeks, 25=Welcomes, 10=Geeks, 30=You, 15=4} The set is: [20=Geeks, 25=Welcomes, 10=Geeks, 30=You, 15=4] 프로그램 2: 정수 값을 문자열 키에 매핑합니다.
// Java code to illustrate the entrySet() method> import> java.util.*;> > public> class> Hash_Map_Demo {> > public> static> void> main(String[] args)> > {> > > // Creating an empty HashMap> > HashMap hash_map => new> HashMap();> > > // Mapping int values to string keys> > hash_map.put(> 'Geeks'> ,> 10> );> > hash_map.put(> '4'> ,> 15> );> > hash_map.put(> 'Geeks'> ,> 20> );> > hash_map.put(> 'Welcomes'> ,> 25> );> > hash_map.put(> 'You'> ,> 30> );> > > // Displaying the HashMap> > System.out.println(> 'Initial Mappings are: '> + hash_map);> > > // Using entrySet() to get the set view> > System.out.println(> 'The set is: '> + hash_map.entrySet());> > }> }> |
산출:
Initial Mappings are: {4=15, Geeks=20, You=30, Welcomes=25} The set is: [4=15, Geeks=20, You=30, Welcomes=25] 메모: 다양한 데이터 유형의 변형 및 조합을 사용하여 모든 유형의 매핑에 대해 동일한 작업을 수행할 수 있습니다.