Anybody , please let me know the issue here. I'm passing the value by copying the Map as it is done in remove function.
package com.codegym.task.task08.task0817;
import java.util.HashMap;
import java.util.Map;
/*
We don't need repeats
Create a Map<String, String> and add ten entries that represent (last name, first name) pairs.
Remove people with the same first name.
Requirements:
1. The program should not display text on the screen.
2. The program should not read values from the keyboard.
3. The createMap() method must create and return a HashMap that has (String, String) elements and contains 10 entries.
4. The removeFirstNameDuplicates() method must remove from the map all people who have the same first name.
5. The removeFirstNameDuplicates() method must call the removeItemFromMapByValue() method.
*/
public class Solution {
public static HashMap<String, String> createMap() {
//write your code here
HashMap<String, String> map = new HashMap<String, String>();
map.put("kumar", "swami");
map.put("verma", "gaurav");
map.put("singh", "tarun");
map.put("yadav", "sonam");
map.put("kaushik", "sunil");
map.put("rathor", "sonali");
map.put("deyol", "raj");
map.put("ping", "suresh");
map.put("sheran", "satish");
map.put("huda", "swaminarayan");
return map;
}
public static void removeFirstNameDuplicates(Map<String, String> map) {
//write your code here
HashMap<String, String> c = new HashMap<String, String>(map);
for (HashMap.Entry<String, String> pair : c.entrySet()){
String firstName =pair.getValue();
removeItemFromMapByValue(c,firstName);
}
}
public static void removeItemFromMapByValue(Map<String, String> map, String value) {
HashMap<String, String> copy = new HashMap<String, String>(map);
for (Map.Entry<String, String> pair : copy.entrySet()) {
if (pair.getValue().equals(value))
map.remove(pair.getKey());
}
}
public static void main(String[] args) {
}
}