Write a Java program to Remove Keys from HashMap.

Write code to remove all the entries having keys multiple of 4 and return the size of the final hashmap. The return type of the output is an integer which is the size of the resultant hashmap.

Input and Output Format

  • The first input corresponds to the size of the hashmap.
  • Input consists of a hashmap<integer, string>.
  • The output is an integer which is the size of the hashmap.

Refer sample output for formatting specifications

Sample Input 1:

3

2

hi

4

hello

12

hello world

Sample Output 1:

1

Sample Input 2:

3

2

hi

4

sdfsdf

3

asdf

Sample Output 2:

2

Remove Keys from HashMap Program in Java

Following are the step to remove keys from HashMap in Java:

  • First, construct an empty HashMap.
  • Now, use a buffered reader to take character input from the user.
  • Input the size of the HashMap.
  • Add elements to the HashMap with both key and value.
  • Now, pass the Hashmap to the getvalues() method.
  • Inside the method, declare and initialize the count value to 0.
  • Now, iterate over the input HashMap, and check if the key is not a multiple of 4. Then, just increment the count value.
  • At last, print the count value.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
 
public static void main(String[] args) throws NumberFormatException, IOException {
HashMap<Integer, String>hm=new HashMap<Integer, String>();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));int n = Integer.parseInt(br.readLine());
for(int i=0;i<n;i++){
  hm.put(Integer.parseInt(br.readLine()), br.readLine());
}

getvalues(hm);
}
public static void getvalues(HashMap<Integer, String> hm) {
int count=0;
Iterator<Integer>itr=hm.keySet().iterator();
while(itr.hasNext())
{
int n=itr.next();
if(n%4!=0)
{

count++;
}
}
System.out.println(count);
}
}

Output

Program to Delete All Elements from HashMap

Here, in order to delete all elements from the HashMap, there is only one method i.e clear(). It would remove all the elements from the HashMap.

package com.testcases;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class HashMapSolution {
    public static void main(String[] args) {
        Map<Integer,String> map = new HashMap<>();
        map.put(1,"Red Mi");
        map.put(2,"Samsung");
        map.put(3,"Iphone");
        map.put(4,"Vivo");
        System.out.println(map);
        map.clear();
        System.out.println(map);
    }
}

Output

Remove Key while Iterating using Java 8

Write a code to remove the specified key from the Map using Java 8 feature.

Input and Output Format

  • The first input corresponds to the key to be removed.
  • The output is the updated HashMap.

Refer sample output for formatting specifications

Sample Input :

3

Sample Output :

{1=Red Mi, 2=Samsung, 3=Iphone, 4=Vivo}
{1=Red Mi, 2=Samsung, 4=Vivo}

Java Program to remove the key while Iterating in HashMap

  • First, create an empty HashMap and add elements to it using the put() method.
  • Then, input the key to be removed.
  • We are using the Lambda function to check if the specified key is equal to the key in the map, then remove it.
  • At last, display the map after removing the key.
package com.testcases;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;

public class HashMapSolution {
    public static void main(String[] args) {
        Map<Integer,String> map = new HashMap<>();
        map.put(1,"Red Mi");
        map.put(2,"Samsung");
        map.put(3,"Iphone");
        map.put(4,"Vivo");
        Scanner scanner = new Scanner(System.in);
        int key =  scanner.nextInt();
        System.out.println(map);
        map.entrySet().removeIf(entry -> (key==entry.getKey()));
        System.out.println(map);

    }
}

Output

Thus, in this way, we learn how to remove keys from the HashMap in Java.