public static void main(String[] args) {
}
package com.codegym.task.task08.task0817;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/*
We don't need repeats
*/
public class Solution {
public static HashMap<String, String> createMap() {
HashMap<String,String> map=new HashMap<>();//write your code here
map.put("watermelon", "melon");
map.put("banana","frui");
map.put("cherry","fruit");
map.put("pear","frut");
map.put("cantaloupe","meln");
map.put("blackberry","frit");
map.put("ginseng","root");
map.put("strawberry","fuit");
map.put("iris","flower");
map.put("potato","tuber");
return map;
}
public static void removeFirstNameDuplicates(Map<String, String> map) {
//write your code here
HashMap<String, String> entry = new HashMap<String,String>(map);
Iterator<Map.Entry<String, String>> it = entry.entrySet().iterator();
int c = 0, no = 0;
while (it.hasNext()) {
String value = it.next().getValue();
//System.out.println(value);
Iterator<HashMap.Entry<String, String>> it2 = map.entrySet().iterator();
while (it.hasNext()) {
String valu = it.next().getValue();
if (value.equals(valu))
c++;
if (c > 1) {
removeItemFromMapByValue(map, valu);
no = 1;
}
}
if(no==1)
removeItemFromMapByValue(map,value);
no=0;
}
}
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) {
}
}