Всем привет! Подскажите, пожалуйста, по коду ниже. Код выдает нужный результат, но проверку не проходит - говорит, что я не удаляю элементы из map.
package com.javarush.task.task08.task0817;
import java.util.*;
/*
Нам повторы не нужны
*/
public class Solution {
public static HashMap<String, String> createMap() {
//напишите тут ваш код
HashMap<String, String> map = new HashMap<>();
map.put("F", "A");
map.put("A1", "A");
map.put("D", "S");
map.put("R", "Q");
map.put("T", "I");
map.put("H", "P");
map.put("L1", "P");
map.put("E1", "P");
map.put("J", "W");
map.put("Y", "H");
return map;
}
public static void removeTheFirstNameDuplicates(Map<String, String> map) {
//напишите тут ваш код
ArrayList<String> set = new ArrayList<>();
HashMap<String, String> copy = new HashMap<String, String>(map);
for(Iterator<Map.Entry<String, String>> it = map.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<String, String> pair = it.next();
if ((Collections.frequency(map.values(), pair.getValue())) > 1) // if(text.equals(pair.getValue()))
{
it.remove();
}
removeItemFromMapByValue(copy,pair.getValue());
}
System.out.println(map);
}
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) {
removeTheFirstNameDuplicates(createMap());
}
}
package com.javarush.task.task08.task0817;
import java.util.*;
/*
Нам повторы не нужны
*/
public class Solution {
public static HashMap<String, String> createMap() {
//напишите тут ваш код
HashMap<String, String> map = new HashMap<>();
map.put("F", "A");
map.put("A1", "A");
map.put("D", "S");
map.put("R", "Q");
map.put("T", "I");
map.put("H", "P");
map.put("L1", "P");
map.put("E1", "P");
map.put("J", "W");
map.put("Y", "H");
return map;
}
public static void removeTheFirstNameDuplicates(Map<String, String> map) {
//напишите тут ваш код
ArrayList<String> set = new ArrayList<>();
HashMap<String, String> copy = new HashMap<String, String>(map);
/* Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry<String, String> pair = iterator.next();
set.add(pair.getValue());
}
System.out.println(set);
int count = 0;*/
/* for (String text : set) {
if ((Collections.frequency(copy.values(), text)) > 1) {
removeItemFromMapByValue(map, text);
}*/
for(Iterator<Map.Entry<String, String>> it = map.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<String, String> pair = it.next();
if ((Collections.frequency(map.values(), pair.getValue())) > 1) // if(text.equals(pair.getValue()))
{
it.remove();
}
removeItemFromMapByValue(copy,pair.getValue());
}
//System.out.println(map);
}
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) {
//removeTheFirstNameDuplicates(createMap());
}
}