The fastest way:
for (Map.Entry<String, Boolean> entry : selectedIds.entrySet()) { entry.setValue(true); }
This code avoids any lookups because it iterates through all the map entries and sets their values ββdirectly.
Note that whenever HashMap.put()
is HashMap.put()
, a key search appears in the internal Hashtable
. Although the code is highly optimized, it nevertheless requires work to calculate and compare hash codes, and then uses the algorithm to finally search for the record (if it exists). It all "works" and consumes CPU cycles.
Java 8 update:
Java 8 introduced the new replaceAll()
method just for this purpose, making the code even simpler:
selectedIds.replaceAll((k, v) -> true);
source share