Using the map ADT A map is a data structure that provides a collection containin
ID: 3605341 • Letter: U
Question
Using the map ADT A map is a data structure that provides a collection containing unique keys associated with a single value. Operations Create empty map Find key in map Find value of a given key, if key exists Insert a key with its associated value, if key does not already exist Remove a key and its associated value · Implement map ADT in file called mapYourLastName.java, to map email addresses (key) to people's names (value) Both can be Strings. class should add at lea when done. Submit your java files only in zip folder. Note from discussion board Just like Queue, you made a constructor and methods, here you define a class Map, decide how you are storing key, value pairs (in trees and lists we used nodes, queues we used an array) Then have a Map0 constructor, a method returning a boolean called findKey, a method returning the value of a given key getValue(key) if the key exists, Then you will make a MapTester file. Iprovided Queue, Stack, but you will write your own here.Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
class Chegg
{
static boolean findKey(Map m,Object key)//Method to check whether key is present or not
{
return m.containsKey(key);
}
static Object getValue(Map m,Object key)//Method to get the value with the associated key
{
return m.get(key);
}
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);//Scanner object to take user input
HashMap m=new HashMap();//HashMap is the implementation class of Map Interface,.Hence we cannot Instantiate Map Interface, rather we need to instantiate HashMap.
//Now we will insert some values in HashMap as key-value pairs
m.put("Adam",100);
m.put("Mesut",10);
m.put("Hazard",7);
m.put("Messi",1);
m.put("Ronaldo",7);
m.put("David",2);
System.out.println("Enter key you want to find");
String key1=sc.next();//Taking user input for key,which we want to find
boolean r1=findKey(m,key1);//Storing result which is return by findKey method in r1
System.out.println("Key= "+key1+" Map= "+m+" Result1= "+r1);//printing the result in r1
System.out.println("Enter key whose value you want to find");
String key2=sc.next();
Object r2=getValue(m,key2);
System.out.println("Key= "+key2+" Map= "+m+" Result2= "+r2);
System.out.println("Enter key which you want to insert in Map");
String key3=sc.next();
//if the key is already present in HashMap,then containsKey will return true and in that case we do not need to insert that key but if it is not present we will insert that key in HashMap
if(m.containsKey(key3)==false)// if key is not present we will insert that key in HashMap
{
m.put(key3,50);
System.out.println("Key= "+key3+" HashMap After key was inserted in HashMap= "+m);
}
System.out.println("Enter key which you want to remove and its associated value");
String key4=sc.next();
m.remove(key4);
System.out.println("key="+key4+" HashMap After key and its associated value was removed = "+m);
}
}
Sample Input:
Adam
Ronaldo
Sachin
Hazard
Sample Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.