HashMap<String, boolean> copy all the keys into HashMap<String, Integer>and initialize values to zero

49,352

Solution 1

Don't think there's much need for anything fancy here:

Map<String, Boolean> map = ...;
Map<String, Integer> newMap = Maps.newHashMapWithExpectedSize(map.size());
for (String key : map.keySet()) {
  newMap.put(key, 0);
}

If you do want something fancy with Guava, there is this option:

Map<String, Integer> newMap = Maps.newHashMap(
    Maps.transformValues(map, Functions.constant(0)));

// 1-liner with static imports!
Map<String, Integer> newMap = newHashMap(transformValues(map, constant(0)));

Solution 2

Looping is pretty easy (and not inelegant). Iterate over the keys of the original Map and put it in them in the new copy with a value of zero.

Set<String> keys = original.keySet();
Map<String, Integer> copy = new HashMap<String, Integer>();
for(String key : keys) {
    copy.put(key, 0);
}

Hope that helps.

Solution 3

final Integer ZERO = 0;

for(String s : input.keySet()){
   output.put(s, ZERO);
}
Share:
49,352
NimChimpsky
Author by

NimChimpsky

side hustle : metriculous.network Spring is too bloated, I created my own web app framework Infrequent tweets What if programming languages were methods to eat an orange?

Updated on July 09, 2022

Comments

  • NimChimpsky
    NimChimpsky almost 2 years

    What is the best way ?

    Just looping through and putting the key and zero, or is there another more elegant or existing library method. I am also using Google's guava java library if that has any useful functionality ?

    Wanted to check if there was anything similar to the copy method for lists, or Map's putAll method, but just for keys.

    • Isac
      Isac over 13 years
      Please, rephrase your question, I couldn't understand it.
    • Vishy
      Vishy over 13 years
      I would just loop through the keys, it would only take three lines of code. If you want it shorter you could write a method to do it.
    • Kevin Bourrillion
      Kevin Bourrillion over 13 years
      Note that if you use a Multiset instead of a Map you don't have to initialize all the keys to zero. The first time you add any number of occurrences of a new key it will initialize it for you.
  • Isac
    Isac over 13 years
    Why create a constant named ZERO? If it doesn't add meaning to the value, why not use 0?
  • pgras
    pgras over 13 years
    @Isac: Not sure but wont Integer.valueOf(0) be evaluated for every loop ?
  • C. K. Young
    C. K. Young over 13 years
    Using new Integer(0) is very wasteful of the heap. Consider using 0 and letting the autoboxing work, or if you detest autoboxing, use Integer.valueOf(0).
  • ColinD
    ColinD over 13 years
    @Isac: Pretty sure that'd be to avoid boxing every iteration, which may be slightly better in terms of performance. I don't feel like it's worth the reduced clarity though.
  • C. K. Young
    C. K. Young over 13 years
    Yes, but so? It gets inlined by the JIT compiler into the cached 0 value every time.
  • Todd
    Todd over 13 years
    I was just trying to give a simple answer. And autoboxing is the same as new Integer(0), is it not? Yes, Integer.valueOf(0) would use less memory.
  • C. K. Young
    C. K. Young over 13 years
    Nope, it does Integer.valueOf(0) (which uses cached boxed values for numbers between -128 and 127).