HackerRank Java- Map




You are given a phone book that consists of people's names and their phone number. After that you will be given some person's name as query. For each query, print the phone number of that person.



  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.io.IOException;
  4. import java.util.HashMap;
  5. class Solution {
  6. public static void main(String[] args) throws IOException {
  7. /* Save input as entries in a HashMap */
  8. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  9. int n = Integer.parseInt(br.readLine());
  10. HashMap<String, Integer> map = new HashMap();
  11. while (n-- > 0) {
  12. String name = br.readLine();
  13. int phone = Integer.parseInt(br.readLine());
  14. map.put(name, phone);
  15. }
  16. /* Read each query and check if its in our HashMap */
  17. String s;
  18. while((s = br.readLine()) != null) {
  19. if (map.containsKey(s)) {
  20. System.out.println(s + "=" + map.get(s));
  21. } else {
  22. System.out.println("Not found");
  23. }
  24. }
  25. br.close();
  26. }
  27. }
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






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