Need help with HashMap | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

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

18th Aug 2020, 9:00 AM
stephen haokip
stephen haokip - avatar
2 Answers
+ 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); }
18th Aug 2020, 9:18 AM
Jayakrishna 🇮🇳
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") );
18th Aug 2020, 11:31 AM
zemiak