Created
January 8, 2020 03:42
-
-
Save parzibyte/151e326a6b59549f4c3bd62b3e2fa6ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Archivo: EjemploHashMap.java | |
* Clase: EjemploHashMap | |
* Autor: parzibyte | |
* Fecha: 1/7/20 9:03 PM | |
* Visita https://parzibyte.me/blog para más tutoriales sobre Java | |
*/ | |
import java.util.HashMap; | |
public class EjemploHashMap { | |
public static void main(String[] args) { | |
HashMap<String, Integer> hashMap = new HashMap<>(); | |
hashMap.put("A", 1); | |
if (hashMap.containsKey("A")) { | |
System.out.printf("Contiene la clave A. Su valor es: %d\n", hashMap.get("A")); | |
} | |
if (hashMap.containsValue(0)) { | |
System.out.println("Contiene el valor 0"); | |
} | |
int aparicionesDeA = hashMap.get("A"); | |
hashMap.remove("A"); | |
// Ahora ponemos varios elementos para imprimirlos | |
hashMap.put("A", 1); | |
hashMap.put("E", 12); | |
hashMap.put("I", 15); | |
hashMap.put("O", 0); | |
hashMap.put("U", 0); | |
for (HashMap.Entry<String, Integer> entry : hashMap.entrySet()) { | |
System.out.printf("Clave: %s. Valor: %d\n", entry.getKey(), entry.getValue()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment