性能:创建从HashMap.values一个ArrayList() [英] Performance: Creating an ArrayList from HashMap.values()
问题描述
问题是付出多大的代价来从HashMap.values()收集一个ArrayList?或独自创造价值集合?
假设Map.size()> 10万。
对象也可以在ArrayList中举行(代替HashMap的)所有其具有在其他部分的影响的时间(元素的修改,容易通过键)。
ArrayList中用于遍历每个第n个元素。 (这就是为什么值集合不能直接使用)。没有修改的迭代过程中完成的。
Question is how much it costs to create an ArrayList from a HashMap.values() Collection? Or creating the values Collection alone? Assuming Map.size() > 100k. Objects could also be held in ArrayList (instead of HashMap) all the time which has implications in other parts (modifications of elements, easy by key). The ArrayList is used to iterate over every n-th element. (That's why the values collection can't be used directly). No modifications are done during the iteration.
推荐答案
HashMap.values()
不返回的ArrayList
值但值
系列。
来源:
public Collection<V> values() {
Collection<V> vs = values;
return (vs != null ? vs : (values = new Values()));
}
值
是类AbstractCollection
。原因值只是引用HashMap中的迭代器。
Values
is an AbstractCollection
. The reason for values is just to reference HashMap's iterator.
您的问题:
问题是多少它的成本
创建从一个ArrayList
HashMap.values()系列?
Question is how much it costs to create an ArrayList from a HashMap.values() Collection?
这是一个线性复杂(如Bozho说),因为
That's a linear complexity (as Bozho said) since
ArrayList<V> valuesList = new ArrayList<V>(hashMap.values());
ArrayList中, ValuesList中
调用集合的HashMap
的toArray()
方法基本上不从0..N(大小)元素的为
循环收藏。
the ArrayList, valuesList
calls the collection hashMap
toArray()
method which essentially does a for
loop from 0..N (size) element in the collection.
希望这有助于。
这篇关于性能:创建从HashMap.values一个ArrayList()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!