logo

Java의 HashMap keySet() 메소드

Java의 java.util.HashMap.keySet() 메소드는 해시 맵에 포함된 핵심 요소 세트를 생성하는 데 사용됩니다. 기본적으로 키의 집합 보기를 반환하거나 새 집합을 만들고 그 안에 키 요소를 저장할 수 있습니다.

통사론:



hash_map.keySet()>

매개변수: 이 메서드는 매개변수를 사용하지 않습니다.

반환 값: 이 메서드는 해시 맵의 키가 포함된 집합을 반환합니다.

아래 프로그램은 java.util.HashMap.keySet() 메소드의 작동을 설명하는 데 사용됩니다.
프로그램 1: 문자열 값을 정수 키에 매핑합니다.



자바 문자열 형식 지정






호환성 테스트
// Java code to illustrate the keySet() 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 keySet() to get the set view of keys> >System.out.println(>'The set is: '> + hash_map.keySet());> >}> }>

스윙이 있는 자바
>

>

산출:

안드로이드 개발자 모드 끄기
 Initial Mappings are: {20=Geeks, 25=Welcomes, 10=Geeks, 30=You, 15=4} The set is: [20, 25, 10, 30, 15]>

프로그램 2: 정수 값을 문자열 키에 매핑합니다.




// Java code to illustrate the keySet() 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 keySet() to get the set view of keys> >System.out.println(>'The set is: '> + hash_map.keySet());> >}> }>

자바 기간
>

>

산출:

 Initial Mappings are: {4=15, Geeks=20, You=30, Welcomes=25} The set is: [4, Geeks, You, Welcomes]>

메모: 다양한 데이터 유형의 변형 및 조합을 통해 모든 유형의 매핑에 대해 동일한 작업을 수행할 수 있습니다.