Norway


Map map = new (collection.());
for (Object obj : collection) {
       map.put(obj., obj.value);
}

The code above wants to make sure that the HashMap doesnR7;t need to be resized, so its sets the initial size to the number of elements that it will use. But, the HashMap implementation doesnR7;t behave like this, it sets its internal threshold to threshold = (int)(capacity * loadFactor), this means that it will resize after 75% of the collection has been inserted into the map, causing extra garbage. Avoid this using the code below:

Map map = new HashMap(1+ (int) (collection.size() / 0.75));



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here