java.util.HashMap.containsKey() 메소드는 특정 키가 HashMap에 매핑되는지 여부를 확인하는 데 사용됩니다. 키 요소를 매개변수로 사용하고 해당 요소가 맵에 매핑되면 True를 반환합니다.
통사론:
Hash_Map.containsKey( key_element )>
매개변수: 이 메서드는 매개변수 하나만 사용합니다. 중요 요소 이는 맵 내에서 매핑을 확인해야 하는 키를 나타냅니다.
반환 값: 이 메서드는 키의 존재가 감지되면 부울 true를 반환하고, 그렇지 않으면 false를 반환합니다.
아래 프로그램은 java.util.HashMap.containsKey() 메소드의 작동을 설명하는 데 사용됩니다.
프로그램 1: 문자열 값을 정수 키에 매핑합니다.
자바
시스
// Java code to illustrate the containsKey() 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);> >// Checking for the key_element '20'> >System.out.println('Is the key>'20'> present? ' +> >hash_map.containsKey(>20>));> >// Checking for the key_element '5'> >System.out.println('Is the key>'5'> present? ' +> >hash_map.containsKey(>5>));> >}> }> |
>
>산출:
Initial Mappings are: {20=Geeks, 25=Welcomes, 10=Geeks, 30=You, 15=4} Is the key '20' present? true Is the key '5' present? false> 프로그램 2: 정수 값을 문자열 키에 매핑합니다.
자바
// Java code to illustrate the containsKey() 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);> >// Checking for the key_element 'Welcomes'> >System.out.println('Is the key>'Welcomes'> present? ' +> >hash_map.containsKey('Welcomes'));> >// Checking for the key_element 'World'> >System.out.println('Is the key>'World'> present? ' +> >hash_map.containsKey('World'));> >}> }> |
>
>산출:
Initial Mappings are: {4=15, Geeks=20, You=30, Welcomes=25} Is the key 'Welcomes' present? true Is the key 'World' present? false> 메모: 다양한 데이터 유형의 변형 및 조합을 사용하여 모든 유형의 매핑에 대해 동일한 작업을 수행할 수 있습니다.
시간 복잡도:
HashMap.containsKey()의 시간 복잡도는 다음과 같습니다. 오(1) ~에 평균 사례 , 그리고 에) 최악의 경우.