Saltar al contenido

¿Cómo funciona HashMap en Java? ejemplo de código

Nuestro team de trabajo ha estado mucho tiempo investigando para dar resolución a tu búsqueda, te brindamos la solución de modo que nuestro objetivo es serte de mucha ayuda.

Ejemplo 1: métodos hashmap de Java

importjava.util.HashMap;// import the HashMap class// instantiate a HashMap instanceHashMap<String,String> capitalCities =newHashMap<String,String>();// SCROLL DOWN FOR LIST OF ALL METHODS --------------------------------------// to use any methods below add the function name to the instance name// General Form: HashMapInstanceName.functionName(parameter list);// Specific Example Using the Put Method: 
capitalCities.put("California","Sacramento");// SCROLL DOWN FOR LIST OF ALL METHODS --------------------------------------1.voidclear()-Removes all of the mappings from this map.2.Objectclone()-Returns a shallow copy of thisHashMap instance: the keys and values themselves are not cloned.3.booleancontainsKey(Object key)-Returnstrueifthis map contains a mapping for the specified key.4.booleancontainsValue(Object value)-Returnstrueifthis map maps one or more keys tothe specified value.5.Vget(Object key)-Returns the value towhich the specified key is mapped, or nullifthis map contains no mapping for the key.6.VgetOrDefault(Object key,V defaultValue)-Returns the value towhich the specified key is mapped, or defaultValue ifthis map contains no mapping for the key.7.booleanisEmpty()-Returnstrueifthis map contains no key-value mappings.8.Set<K>keySet()-Returns a Set view of the keys contained in this map.9.Vput(K key,V value)-Associates the specified value withthe specified key in this map.10.Vremove(Object key)-Removes the mapping for the specified key from this map if present.11.booleanremove(Object key,Object value)-Removes the entry for the specified key only if it is currently mapped tothe specified value.12.Vreplace(K key,V value)-Replaces the entry for the specified key only if it is currently mapped tosome value.13.booleanreplace(K key,V oldValue,V newValue)-Replaces the entry for the specified key only if currently mapped tothe specified value.14.intsize()-Returns the number of key-value mappings in this map.15.Collection<V>values()-Returns a Collection view of the values contained in this map.

Ejemplo 2: sintaxis java hashmap

// Import the HashMap classimportjava.util.HashMap;// First Example// Create a HashMap object called capitalCitiesHashMap<String,String> capitalCities =newHashMap<String,String>();// Add keys and values (Country, City)
    	capitalCities.put("England","London");
    	capitalCities.put("Germany","Berlin");
    	capitalCities.put("Norway","Oslo");
    	capitalCities.put("USA","Washington DC");System.out.println(capitalCities);//Second Example// Create a HashMap object called stGradeMap<String,Integer> stGrade =newHashMap<String,Integer>();// Insert elements 
       	stGrade.put("aaron",newInteger(90)); 
       	stGrade.put("isaac",newInteger(100)); 
       	stGrade.put("john",newInteger(35)); 
       	stGrade.put("mohammad",newInteger(100));// Get value
	   	stGrade.get("mohammad");// returns 100
		stGrade.get("aaron");// returns 90
		stGrade.get("john");// returns 35
		stGrade.get("isaac");// returns 100

Finalizando este artículo puedes encontrar las observaciones de otros sys admins, tú además tienes la opción de mostrar el tuyo si dominas el tema.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *