2

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?

Maroun
  • 94,125
  • 30
  • 188
  • 241
NPKR
  • 5,368
  • 4
  • 31
  • 48
  • 8
    Why can you not use a for loop? – Patashu May 16 '13 at 13:17
  • What's so bad about `for` loop? I haven't heard of any utils like that. Anyway if there are any, they are most likely to contain a loop inside. – svz May 16 '13 at 13:17
  • Not in JDK, and I don't suppose you prefer adding a whole new JAR to your project just to avoid writing this one-liner. – Marko Topolnik May 16 '13 at 13:18
  • 5
    In particular, *something* is going to have to loop - and you only have to write the (tiny) method once... – Jon Skeet May 16 '13 at 13:19
  • not that i know of. i beljeve you will have to use a loop. shouldn't be too complex, for loop on Hash.getKeys(). – donfede May 16 '13 at 13:19
  • I don't think there is a way to change all values in a `HashMap` without a loop. I think your best shot is to use the `entrySet()` method with a for-each loop. This method is highly optimized and easy to use. – 629 May 16 '13 at 13:23
  • Afterwards, do the values need to be independent? – Andy Thomas May 16 '13 at 13:28
  • Make sure every key in your HashMap points to the same object, et voila! – vikingsteve May 16 '13 at 13:29
  • I think that the only purpose of this question is to downvote the answerers. It should be actually closed as not constructive. – Danubian Sailor May 16 '13 at 14:01
  • thanks for all your replys. – NPKR May 16 '13 at 14:03
  • I think this question needs to be edited (mention of "no loop" should be removed) and reopened. 10k views is a sign that people stumble upon this problem a lot. – leventov Feb 25 '19 at 21:19
  • BTW, since Java 8 the ideomatic way to do this is calling `Map.replaceAll((k, v) -> myConstantValue)` – leventov Feb 25 '19 at 21:19

5 Answers5

19
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 );
}
BruceRV
  • 80
  • 6
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
2

You can use iterator on entrySet:

Iterator it = yourMap.entrySet().iterator();
Map.Entry keyValue;
while (it.hasNext()) {
    keyValue = (Map.Entry)it.next();
    //Now you can have the keys and values and easily replace the values...
}

Note that the internal implementation of the iterator still uses a for loop :)

Maroun
  • 94,125
  • 30
  • 188
  • 241
  • Serial downvoter - Why? – Maroun May 16 '13 at 13:31
  • Note - I didn't downvote. However, this 500+ answer(http://stackoverflow.com/questions/46898/how-do-i-iterate-over-each-entry-in-a-map) indicates another, perhaps `cleaner?` way, to iterate over a map. – Kevin Meredith May 16 '13 at 13:33
  • 3
    OP: "*without for loop?*" - I thought he wants to use an iterator. I hate it to be downvoted without mentioning why. I want to learn from my mistakes.. – Maroun May 16 '13 at 13:39
1

Try to use Guava:

Collections2.transform(stringStringHashMap.values(), new Function<String, String>() {
  @Override
  public String apply(java.lang.String s) {
    return "modified string";
  }
});
Igor Konoplyanko
  • 9,176
  • 6
  • 57
  • 100
  • Does `apply()` work like `map` in Haskell? – Kevin Meredith May 16 '13 at 13:35
  • thanks for all your replys – NPKR May 16 '13 at 14:03
  • 1
    @Kevin: That part is just boilerplate since Java doesn't have lambda expressions, so you have to make an anonymous class and implement the `apply()` method instead. The important bit here is `transform()`, which is indeed like Haskell's `map`. – hammar May 29 '13 at 05:45
0

Extend the HashMap and make your implementation return your magic value if some condition is set.

heikkim
  • 2,955
  • 2
  • 24
  • 34
  • Extending the entire `HashMap` seems like overkill. Why is it necessary to extend it? – Kevin Meredith May 16 '13 at 13:44
  • I don't understand what you mean by "entire hashmap" but overriding the get-method & other value acquirers would allow the implementation return the same value for any key (or the actual value for the key) based on some condition - this achieves the same behaviour as replacing all values. It clearly is not the same thing as actually replacing all values. – heikkim May 16 '13 at 13:51
0

It's not clear how you plan to set it to a single value, but you can call putAll on the HashMap as demonstrated from this answer:

Map tmp = new HashMap(patch);
tmp.keySet().removeAll(target.keySet());
target.putAll(tmp);

patch is the map you are adding to target.

patch could be a HashMap containing the same value for all keys....

Community
  • 1
  • 1