package com.codegym.task.task08.task0817;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.HashSet;
/*
We don't need repeats
*/
public class Solution {
public static HashMap<String, String> createMap() {
//write your code here
HashMap<String, String> map = new HashMap<String, String>();
map.put("a", "b");
map.put("c", "d");
map.put("e", "f");
map.put("g", "f");
map.put("r", "j");
map.put("l", "k");
map.put("m", "n");
map.put("o", "p");
map.put("q", "i");
map.put("s", "t");
return map;
}
public static void removeFirstNameDuplicates(Map<String, String> map) {
HashSet<String> set = new HashSet<String>();
Iterator<Map.Entry<String,String>> i = map.entrySet().iterator();
while(i.hasNext()){
Map.Entry<String, String> pair = i.next();
set.add(pair.getValue());
}
for(String text : set)
{
removeItemFromMapByValue(map,text);
}
}
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) {
}
}
package com.codegym.task.task08.task0817;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.HashSet;
/*
We don't need repeats
*/
public class Solution {
public static HashMap<String, String> createMap() {
//write your code here
HashMap<String, String> map = new HashMap<String, String>();
map.put("a", "b");
map.put("c", "d");
map.put("e", "f");
map.put("g", "f");
map.put("r", "j");
map.put("l", "k");
map.put("m", "n");
map.put("o", "p");
map.put("q", "i");
map.put("s", "t");
return map;
}
public static void removeFirstNameDuplicates(Map<String, String> map) {
//write your code here
//Iterator i = map.entrySet().iterator();
// while(i.hasNext()){
//HashMap.Entry<String, String> p = i.next();
// for(Map.Entry<String,String> pair: map.entrySet())
// removeItemFromMapByValue(map, pair.getValue());
//}
// HashMap<String, String> newMap = new HashMap<String, String>();
HashSet<String> set = new HashSet<String>();
Iterator<Map.Entry<String,String>> i = map.entrySet().iterator();
while(i.hasNext()){
Map.Entry<String, String> pair = i.next();
set.add(pair.getValue());
}
// newMap.putAll(map);
// Iterator<Map.Entry<String, String>> newiterator = newMap.entrySet().iterator();
/* int i = 0;
while (newiterator.hasNext())
{
Map.Entry<String, String> newpair = newiterator.next();
i++;
int j = 0;
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext())
{
j++;
Map.Entry<String, String> pair = iterator.next();
if (i != j)
if (newpair.getValue().equals(pair.getValue()))
set.add(newpair.getValue());
}
}
*/
for(String text : set)
{
removeItemFromMapByValue(map,text);
}
}
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) {
}
}