Java Collection | Set 3 (HashMap) Part-1
Submissions: 859   Accuracy:


  Difficulty: Basic   Marks: 1

HashMap is a part of collection in Java since 1.2. It provides the basic implementation of Map interface of Java. It stores the data in (Key, Value) pairs. To access a value you must know its key, otherwise, you can’t access it. HashMap is known as HashMap because it uses a technique Hashing. Try this problem using HashMap.

Given n key - value pairs and a seperate key in next line. If this key is found in the previous n keys then print its value otherwise print "-1".

Input: First line of input contains a single integer T which denotes the number of test cases. T test cases follows, first line of each test case contains a integer n. Second line of each test case consists of n key(String)-value(Integer) pairs space separated. Third and last line of each test case consists of a single key(String).

Output: If the key is found in the HashMap print its value otherwise print "-1".


1<=n,|Length of key|,value<=1000


sak 5 varun 7 vijay 3
csdb 4 dsh 5 askj 8 adfs 9


** For More Input/Output Examples Use 'Expected Output' option **

Contributor: S_A
Author: saksham0751

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

Need help with your code? Please use, generate link and share the link here.

to report an issue on this page.