Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update all values at a time in HashMap [closed]

Tags:

java

hashmap

I have a HashMap with some Keys - Values.

On some condition I want to update all values to single value regardless of keys.

Is there any util or predefined methods to do this, without for loop?

Any suggestions?

like image 301
NPKR Avatar asked Nov 24 '25 06:11

NPKR


1 Answers

if (yourCondition) { 
      for (Map.Entry<String, String> entry : map.entrySet()) {
          map.put(entry.getKey(), MY_VALUE);
      }
}

Or for java 8 or higher (without a loop)

if (yourCondition) { 
      map.replaceAll( (k,v)->v=MY_VALUE );
}
like image 81
Kevin Meredith Avatar answered Nov 25 '25 19:11

Kevin Meredith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!