HashMap Operations | Practice | GeeksforGeeks

Implement different operations on Hashmap. Different types of queries will be provided.
A query can be of four types:
1. a x y (adds an entry with key x and value y to the Hashmap)
2. b x (print value of  x if present in the Hashmap else print -1. )
3. c (prints the size of the Hashmap)
4. d x (removes an entry with key x from the Hashmap)
Example 1 : 
Input:
5 
a 1 2 a 66 3 b 66 d 1 c 

Output:
3 1 

Explanation :
There are five queries. Queries are performed in this order
1. a 1 2  --->  map has a key 1 with value 2
2. a 66 3 ---> map has a key 66 with value 3
3. b 66   ---> prints the value of key 66 if its present in the map ie 3.
4. d 1    ---> removes an entry from map with key 1
5. c      ---> prints the size of the map ie 1
Example 2 :
Input: 
3 
a 1 66 b 5 c

Output: 
-1 1

Explanation :
There are three queries. Queries are performed in this order
1. a 1 66 ---> adds a key 1 with a value of 66 in the map
2. b 5    --->  since the key 5 is not present in the map hence -1 is printed.
3. c      ---> prints the size of the map ie 1
Your Task:
You are required to complete the following functions:
add_Value : Takes HashMap, x, y as arguments and maps x as key and y as its value. Does not return anything.
find_value : Takes HashMap and x as arguments. If HM contains x key then return the value, else return -1.
getSize : Takes HashMap as argument and just returns its size.
removeKey : Takes HashMap and x as arguments and removes x if it exists. Does not return anything.
Constraints:
1 <= Q <= 100
 
 
0
indiakamanthan2 months ago
class GfG
{
    /*Inserts an entry with key x and value y in map */
    void add_Value(HashMap<Integer,Integer> hm, int x, int y)
    {
	//Your code here
	hm.put(x,y);
    }
	
    /*Returns the value with key x from the map */
    int find_value(HashMap<Integer, Integer> hm, int x)
    {
        //Your code here
    if(hm.containsKey(x))
    return hm.get(x);
    return -1;
    }
	
    /*Returns the size of the map */
    int getSize(HashMap<Integer, Integer> hm)
    {
	//Your code here
    return hm.size();
    }
		
    /*Removes the entry with key x from the map */	
    void removeKey(HashMap<Integer, Integer> hm, int x)
    {
	//Your code here
	hm.remove(x);
    }
}
0
pankajkumarravi6 months ago
*********************** Java Logic ***********************
/*Inserts an entry with key x and value y in map */void add_Value(HashMap<Integer,Integer> hm, int x, int y) {    //Your code here    hm.put(x,y);}/*Returns the value with key x from the map */int find_value(HashMap<Integer, Integer> hm, int x) {    //Your code here    if (hm.containsKey(x))    return  hm.get(x);    else         return -1;}/*Returns the size of the map */int getSize(HashMap<Integer, Integer> hm) {    //Your code here    return hm.size();}/*Removes the entry with key x from the map */void removeKey(HashMap<Integer, Integer> hm, int x) {    //Your code here    hm.remove(x);}
0
18r01a05f07 months ago
class GfG {
void add_Value(HashMap<Integer, Integer> hm, int x, int y) { HashMap<Integer, Integer> hm1 = new HashMap<>(); hm.put(x, y);}
int find_value(HashMap<Integer, Integer> hm, int x) { if (hm.containsKey(x)) {  return hm.get(x); } else {  return -1; }
}
int getSize(HashMap<Integer, Integer> hm) { return hm.size();}
void removeKey(HashMap<Integer, Integer> hm, int x) { hm.remove(x);}}
0
MAYUR.xxivk11 months ago
MAYUR.xxivk
https://uploads.disquscdn.c...
0
Amir Ansari1 year ago
Amir Ansari
Correct AnswerExecution Time:0.30
/*Inserts an entry with key x and value y in map */    void add_Value(HashMap<integer,integer> hm, int x, int y)    {//Your code here      hm.put(x,y);    }
    /*Returns the value with key x from the map */    int find_value(HashMap<integer, integer=""> hm, int x)    {        //Your code here        int result = hm.get(x) == null ? -1: hm.get(x);
        return result;    }
    /*Returns the size of the map */    int getSize(HashMap<integer, integer=""> hm)    {//Your code here  return hm.size();    }
    /*Removes the entry with key x from the map */    void removeKey(HashMap<integer, integer=""> hm, int x)    {//Your code here        if( hm.get(x) != null){            hm.remove(x);        }    }
0
Paurash Dewangan1 year ago
Paurash Dewangan
class GfG{    void add_Value(HashMap<integer,integer> hm, int x, int y)    {hm.put(x,y);    }    int find_value(HashMap<integer, integer=""> hm, int x)    {        if(hm.containsKey(x)){            return hm.get(x);        }else{            return -1;        }    }    int getSize(HashMap<integer, integer=""> hm)    {return hm.size();    }    void removeKey(HashMap<integer, integer=""> hm, int x)    {hm.remove(x);    }}
0
Piyush Raj2 years ago
Piyush Raj
void add_Value(HashMap<integer,integer> hm, int x, int y)    { hm.put(x,y)    }     int find_value(HashMap<integer, integer=""> hm, int x)    {        //Your code here        if(hm.containsKey(x))         return hm.get(x);        else         return -1;    }  int getSize(HashMap<integer, integer=""> hm)    {        return hm.size();    }  void removeKey(HashMap<integer, integer=""> hm, int x)    { if(hm.containsKey(x))  hm.remove(x);    }
0
Riya kashyap
This comment was deleted.
0
Vikram Shekhawat
This comment was deleted.
0
Ivan Petrov
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.