I'm trying to implement an outer for-each loop that selects each value of the Hash-map, and an inner for-each loop that then compares each value with the rest of the values. Please help.
package com.codegym.task.task08.task0817;
import java.util.HashMap;
import java.util.Map;
import java.util.Collection;
/*
We don't need repeats
*/
public class Solution {
public static HashMap<String, String> createMap() {
HashMap<String, String> hmap = new HashMap<>();
hmap.put("A", "K");
hmap.put("B", "L");
hmap.put("C", "M");
hmap.put("D", "K");
hmap.put("E", "O");
hmap.put("F", "P");
hmap.put("G", "K");
hmap.put("H", "R");
hmap.put("I", "S");
hmap.put("J", "K");
return hmap;
}
public static Map<String, String> removeFirstNameDuplicates(Map<String, String> map) {
HashMap<String, String> copy = new HashMap<String, String>(map);
for (Map.Entry<String, String> searchName : copy.entrySet()) {
for (Map.Entry<String, String> pair : copy.entrySet()) {
if (pair.getValue().equals(searchName.getValue()))
map.remove(pair.getKey());
}
}
return 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) {}
}
Nested for-each loop?
Under discussion
Comments (1)
- Popular
- New
- Old
You must be signed in to leave a comment
Misiu
9 August 2020, 21:20
1. You remove all elements of map.
2. You don't use removeItemFromMapByValue() method.
0