Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite0
Need help with HashMap
In the code snippet below the method removeItemFromMap removes the value that is less than 500 from the map. It doesn't return anything. How shall i access the method(removeItemFromMap) in which all values less than 500 are removed. https://code.sololearn.com/cQC8khD1H503/?ref=app
2 Réponses
+ 1
As you tried, just call the method.. And print map again to see differences...
public static void main(String[] args)
{
HashMap<String,Integer> m= createMap();
for(HashMap.Entry<String, Integer>pair : m.entrySet())
System.out.println(pair.getKey() + " : " + pair.getValue());
removeItemFromMap(m);
for(HashMap.Entry<String, Integer>pair : m.entrySet())
System.out.println(pair.getKey() + " : " + pair.getValue());
//System.out.println(m);
}
0
parameter for remove() is key, not value so this is enough:
map.remove(pair.getKey() );
//map.remove(pair.getValue() );
the alternative way to print a map is
System.out.println(m.toString() .replace(",", ",\n") );