Java Day 8: Dictionaries and Maps




Given n names and phone numbers, assemble a phone book that maps friends' names to their respective phone numbers. You will then be given an unknown number of names to query your phone book for. For each name queried, print the associated entry from your phone book on a new line in the form name=phoneNumber; if an entry for name is not found, print Not found instead.



  1. //Complete this code or write your own from scratch
  2. import java.util.*;
  3. import java.io.*;
  4. class Solution{
  5. public static void main(String []argh){
  6. Scanner in = new Scanner(System.in);
  7. int n = in.nextInt();
  8. Map<String,Integer> myMap = new HashMap<String,Integer>();
  9. for(int i = 0; i < n; i++){
  10. String name = in.next();
  11. int phone = in.nextInt();
  12. // Write code here
  13. myMap.put(name, phone);
  14. }
  15. while(in.hasNext()){
  16. String s = in.next();
  17. // Write code here
  18. if (myMap.get(s)!=null)
  19. System.out.println(s + "=" + myMap.get(s) );
  20. else
  21. System.out.println("Not found");
  22. }
  23. in.close();
  24. }
  25. }
Please click on the like button if it worked

Solution not working or have any suggestions? Please send an email to [email protected]


donate a cup of tea :)


Join Our Facebook Group

Share this solution


download android app

Popular HackerRank Java Solutions








codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube