Testea a ver
package testing.hashMap;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class HashMap_1 {
private Map<String, Integer> hashMap = new HashMap<String,Integer>();
public HashMap_1() {
hashMap.put("prueba", 1);
hashMap.put("prueba2", 2);
println("Hash Map inicial");
hashMap.forEach((key,valor) -> println("Key: "+key+" Valor: "+valor));
//java 7
for(Iterator<Map.Entry<String, Integer>> it = hashMap.entrySet().iterator(); it.hasNext(); ) {
Map.
Entry<String, Integer
> entry
= it.
next(); if(entry.getKey().equals("prueba2")) {
it.remove();
}
}
//java 8
hashMap.keySet().removeIf(e -> (e.equals("prueba2")));
hashMap.forEach((key,valor) -> println("Luego de Remover:\nkey "+key+" Valor: "+valor));
}
private static
<T
> void println
(final T s
){System.
out.
println(s
);} public static void main
(String ...
aasas) { new HashMap_1();
}
}
Salida en consola
Luego de Remover:
key prueba Valor: 1