0
I can't solve age dependent practice after Java hashmap tutorial. Can anyone help me.
6 Respuestas
+ 4
Since the array contains keys of the hashmap, you can use the keys to get the values associated with each of them. Add the following to the for loop -
int age = ages.get(emp);
if(age < ageLimit)
ages.remove(emp);
+ 2
What's the question of the practice task?
+ 1
import java.util.HashMap;
import java.util.Scanner;
public class Main {
public static void main(String[ ] args) {
Scanner scanner = new Scanner(System.in);
HashMap<String, Integer> ages = new HashMap<String, Integer>();
ages.put("David", 22);
ages.put("Tom", 23);
ages.put("Robert", 32);
ages.put("Alice", 21);
ages.put("Sophie", 19);
ages.put("Maria", 24);
ages.put("John", 28);
String[] nameArr = new String[ages.size()];
nameArr = ages.keySet().toArray(nameArr);
int ageLimit = scanner.nextInt();
for (String emp : nameArr)
{
//your code goes here
}
System.out.println(ages);
}
}
0
The program you are given defines and outputs HashMap, where the names of employees are stored as keys, and their ages as values.
It also takes N number from user as age limit. Write code to delete all the employees whom age is less than N number.
The line of code for the output of HashMap object is already provided.
Sample Input
25
Sample Output
{Robert=32, John=28}
Hint
Use get() method to access values in the HashMap and remove() to delete them.
0
Make a look again at the hashmap
Take the tutorial
0
Thank You < Soumik