在对列表进行索引时,最佳的HashMap初始容量

9 浏览
0 Comments

在对列表进行索引时,最佳的HashMap初始容量

我有一个列表(List list),我想使用一个映射(HashMap map)根据它们的id对其对象进行索引。我总是在HashMap的构造函数中使用list.size()作为初始容量,如下面的代码所示。在这种情况下,这是最好的初始容量吗?

注意:我永远不会向映射中添加更多的项。

List list = myList;

Map map = new HashMap(list.size());

for(T item : list) {

map.put(item.getId(), item);

}

0