- Prepare
- Java
- Data Structures
- Java Map
- Discussions
Java Map
Java Map
+ 0 comments **Solution Java : **
class Solution{ public static void main(String []argh) { Scanner in = new Scanner(System.in); int n=in.nextInt(); in.nextLine(); Map<String, Integer> mp = new HashMap<String,Integer>(); for(int i=0;i<n;i++) { String name=in.nextLine(); int phone =in.nextInt(); in.nextLine(); mp.put(name,phone); } while(in.hasNext()) { String s = in.nextLine(); if(mp.containsKey(s)){ String value =mp.get(s).toString(); System.out.println(s+ "=" +value); }else{ System.out.println("Not found"); } } } }
+ 0 comments class Solution{ public static void main(String []argh) { Scanner in = new Scanner(System.in); Map phoneMap = new HashMap<>(); int n=in.nextInt(); in.nextLine(); for(int i=0;i
+ 0 comments Super simple and self explainatory solution:
import java.io.*; import java.util.*; public class Solution { public static void main(String args[]) { Scanner in = new Scanner(System.in); int rows = in.nextInt(); in.nextLine(); Map<String, String> contacts = new HashMap<>(); for(int i = 0; i < rows; i++) { String name = in.nextLine(); String number = in.nextLine(); contacts.put(name, number); } while(in.hasNext()) { String name = in.nextLine(); if (contacts.get(name) == null) { System.out.println("Not found"); } else { System.out.println(String.format("%s=%s", name, contacts.get(name))); } } } }
+ 0 comments //Complete this code or write your own from scratch import java.util.; import java.io.;
class Solution{ public static void main(String []argh) { Scanner in = new Scanner(System.in); int n=in.nextInt(); in.nextLine(); HashMap map = new HashMap<>();
for(int i=0;i<n;i++) { String name=in.nextLine(); int phone=in.nextInt(); in.nextLine(); map.put(name, phone); } while(in.hasNext()) { String s=in.nextLine(); if(map.get(s)!=null){ System.out.println(s+"="+map.get(s)); }else{ System.out.println("Not found"); } } }
}
+ 1 comment import java.util.HashMap; import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); scanner.nextLine();
HashMap<String, String> phoneBook = new HashMap<>(); // Read n phone book entries for (int i = 0; i < n; i++) { String name = scanner.nextLine(); String phoneNumber = scanner.nextLine(); phoneBook.put(name, phoneNumber); } // Look up phone numbers for m queries while (scanner.hasNext()) { String name = scanner.nextLine(); String phoneNumber = phoneBook.get(name); if (phoneNumber != null) { System.out.println(name + "=" + phoneNumber); } else { System.out.println("Not found"); } } scanner.close(); }
}
Sort 318 Discussions, By:
Please Login in order to post a comment