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:

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 in Java.

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));
//Scanner sc = new Scanner(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;
HashMap<Integer, String>hm1=new HashMap<Integer, String>();
Iterator<Integer>itr=hm.keySet().iterator();
while(itr.hasNext())
{
int n=itr.next();
if(n%4!=0)
{

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

Add a Comment

Your email address will not be published. Required fields are marked *

14 − 1 =