HashMap<String, boolean> copy all the keys into HashMap<String, Integer>and initialize values to zero
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);
}
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, 2022Comments
-
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 over 13 yearsPlease, rephrase your question, I couldn't understand it.
-
Vishy over 13 yearsI 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 over 13 yearsNote 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 over 13 yearsWhy create a constant named ZERO? If it doesn't add meaning to the value, why not use 0?
-
pgras over 13 years@Isac: Not sure but wont Integer.valueOf(0) be evaluated for every loop ?
-
C. K. Young over 13 yearsUsing
new Integer(0)
is very wasteful of the heap. Consider using 0 and letting the autoboxing work, or if you detest autoboxing, useInteger.valueOf(0)
. -
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 over 13 yearsYes, but so? It gets inlined by the JIT compiler into the cached 0 value every time.
-
Todd over 13 yearsI 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 over 13 yearsNope, it does
Integer.valueOf(0)
(which uses cached boxed values for numbers between -128 and 127).