Hi, as written in the title, the second to last condition is not fulfilled. I have gone through the program on paper and pen and for me it makes sense, it should work.
Also, when I run the three lines in void main, for line " //removeFirstNameDuplicates(createMap()); " I get the attached error.
public class Solution {
public static HashMap<String, String> createMap() {
//write your code here
HashMap<String, String> namesMap = new HashMap<>();
for (int i = 0; i < 8; i++){
namesMap.put(i+"lastName", "firstName"+i+1);
}
namesMap.put("lastName", "firstNameSame");
namesMap.put("lastName1", "firstNameSame");
return namesMap;
}
public static void removeFirstNameDuplicates(Map<String, String> map) {
//write your code here
Iterator<Map.Entry<String,String>> iteratorForFirstName = map.entrySet().iterator();
while (iteratorForFirstName.hasNext())
{
Map.Entry<String, String> firstMapPair = iteratorForFirstName.next();
String firstPairValue = firstMapPair.getValue();
int count = Collections.frequency(map.values(), firstPairValue);
if (count > 0)
{
removeItemFromMapByValue(map, firstPairValue);
count = 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) {
//System.out.println(createMap());
//removeFirstNameDuplicates(createMap());
//System.out.println(createMap());
}
}
package com.codegym.task.task08.task0817;
import java.util.Collections;
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() {
//write your code here
HashMap<String, String> namesMap = new HashMap<>();
for (int i = 0; i < 8; i++){
namesMap.put(i+"lastName", "firstName"+i+1);
}
namesMap.put("lastName", "firstNameSame");
namesMap.put("lastName1", "firstNameSame");
return namesMap;
}
public static void removeFirstNameDuplicates(Map<String, String> map) {
//write your code here
Iterator<Map.Entry<String,String>> iteratorForFirstName = map.entrySet().iterator();
while (iteratorForFirstName.hasNext())
{
Map.Entry<String, String> firstMapPair = iteratorForFirstName.next();
String firstPairValue = firstMapPair.getValue();
// for (String valueForComparison : map.values())
int count = Collections.frequency(map.values(), firstPairValue);
/* if (firstPairValue.equals(map.get(valueForComparison)))
{
count++;
}
*/
if (count > 0)
{
removeItemFromMapByValue(map, firstPairValue);
count = 0;
}
//condition to delete the keys whose values are repeated;
}
}
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) {
// System.out.println(createMap());
// removeFirstNameDuplicates(createMap());
// System.out.println(createMap());
}
}