-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveRepeatValueFromMap.java
More file actions
65 lines (54 loc) · 2.25 KB
/
RemoveRepeatValueFromMap.java
File metadata and controls
65 lines (54 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/*
Нам повторы не нужны
Создать словарь (Map<String, String>) занести в него десять записей по принципу "фамилия" - "имя".
Удалить людей, имеющих одинаковые имена.
*/
public class Solution {
public static Map<String, String> createMap() {
Map<String, String> myHashMap = new HashMap<>();
myHashMap.put("Ivanov", "Egor");
myHashMap.put("Petrov", "Zaim");
myHashMap.put("Semenov", "Ivan");
myHashMap.put("Andreev", "Nick");
myHashMap.put("Nickolaev", "Egor");
myHashMap.put("Karpov", "Andrey");
myHashMap.put("Denisov", "Petr");
myHashMap.put("Egorov", "Egor");
myHashMap.put("Vorobiev", "Lol");
myHashMap.put("Ermakov", "Alex");
return myHashMap;
}
public static void removeTheFirstNameDuplicates(Map<String, String> map) {
Iterator<Map.Entry<String, String>> myIterator = map.entrySet().iterator();
ArrayList<String> valuesForDelete = new ArrayList<>();
ArrayList<String> alreadyDeletedValues = new ArrayList<>();
while (myIterator.hasNext()) {
valuesForDelete.add(myIterator.next().getValue());
}
for (int i = 0; i < valuesForDelete.size(); i++) {
for (int j = i + 1; j < valuesForDelete.size(); j++ ) {
if (valuesForDelete.get(i).equals(valuesForDelete.get(j))) {
if (!alreadyDeletedValues.contains(valuesForDelete.get(i))) {
removeItemFromMapByValue(map, valuesForDelete.get(i));
alreadyDeletedValues.add(valuesForDelete.get(i));
break;
}
}
}
}
}
public static void removeItemFromMapByValue(Map<String, String> map, String value) {
Map<String, String> copy = new HashMap<>(map);
for (Map.Entry<String, String> pair : copy.entrySet()) {
if (pair.getValue().equals(value)) {
map.remove(pair.getKey());
}
}
}
public static void main(String[] args) {
}
}