devxlogo

January 17, 2018

Avoiding the HashMap Size Trap

Map map = new HashMap(collection.size());for (Object obj : collection) { map.put(obj.key, obj.value);} The code above wants to make sure that the HashMap doesn’t need to be resized, so its sets