public static void main(String[] args) {
// Crear una función a la que le pasamos un hashmap
// De cadenas y números y nos elimine los elementos
// que tengan un valor impar
HashMap<String, Integer> numeros = new HashMap<String, Integer>();
numeros.put("a", 1);
numeros.put("b", 2);
numeros.put("c", 3);
numeros.put("d", 4);
HashMap<String, Integer> res = fueraPares(numeros);
System.out.println(numeros);
System.out.println(res);
System.out.println(fueraParesSin(numeros));
}
public static HashMap<String, Integer> fueraPares(HashMap<String, Integer> lista) {
HashMap<String, Integer> temp = (HashMap<String, Integer>) lista.clone();
for (Entry<String, Integer> par : lista.entrySet()) {
if (par.getValue() % 2 == 0) {
temp.remove(par.getKey());
}
}
return temp;
}
public static HashMap<String, Integer> fueraParesSin(HashMap<String, Integer> lista) {
HashMap<String, Integer> temp = new HashMap<String, Integer>();
for (Entry<String, Integer> par : lista.entrySet()) {
if (par.getValue() % 2 != 0) {
temp.put(par.getKey(),par.getValue());
}
}
return temp;
}