In Validator, we usually use CollectionHelper.newHashMap(int initialCapacity) to avoid the resizing of the HashMap after its initialization.
The issue is that it's doing more harm than good because the default load factor is of 0.75 so the initial size of the map is always smaller than what we expect and it's always resized.
We should rename the parameter to expectedSize instead of initialCapacity and calculate the size taking into account the default load factor.
Typically, we use the following pattern to initialize a correctly sized Map: {code} Given a List<String> myList, we create the Map with: Map<String, Object> myMap = CollectionHelper.newHashMap( myList.size() ); for ( String element : myList ) { myMap.put( element, object ); } {code}
Currently the Map is created with an initial capacity of {{myList.size()}} but as the load factor is of 0.75, when we try to put {{myList.size()}} elements in it, it is systematically resized. This is totally counter productive as the purpose was to avoid the resizing of the Map.
Same for {{Set}} s and {{List}}s .
See for instance {{Lists.newArrayListWithExpectedSize}} of Guava. |
|