Java 性能:从 HashMap.values() 创建一个 ArrayList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4255075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:04:45  来源:igfitidea点击:

Performance: Creating an ArrayList from HashMap.values()

javaperformancecollectionsarraylisthashmap

提问by beginner_

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 需要多少成本?还是单独创建值集合?假设 Map.size() > 100k。对象也可以一直保存在 ArrayList(而不是 HashMap)中,这对其他部分有影响(元素的修改,按键容易)。ArrayList 用于迭代每个第 n 个元素。(这就是不能直接使用 values 集合的原因)。迭代期间不进行任何修改。

回答by Bozho

You can use an Iteratorto skip elements - just call next()many times.

您可以使用 anIterator来跳过元素 - 只需next()多次调用即可。

Creating a list of any collection has a linear complexity.

创建任何集合的列表具有线性复杂性。

回答by luckyluke

You can create Your own HashMap, that holds the Arraylist collection of values directly (i do not believe that HashMap does it for free, the data structure for it is different). But this requires some additional coding from Your side.

你可以创建你自己的 HashMap,它直接保存 Arraylist 值集合(我不相信 HashMap 是免费的,它的数据结构是不同的)。但这需要你方的一些额外编码。

回答by Peter Knego

HashMapinternally stores values in a Collection values. Take a look at the source codeof AbstractMap, the parent of HashMap.

HashMap在内部将值存储在 Collection 中values。看看的源代码AbstractMap,父HashMap

So HashMap.values()directly returns a Collection. There is no computation or data copying done. It's as fast as it can be.

所以HashMap.values()直接返回一个Collection. 没有完成计算或数据复制。它尽可能快。

Just get the values and then do a for loop:

只需获取值,然后执行 for 循环:

int n = 5;  // every 5th element
Object[] values = hashMap.values().toArray();
int size = values.length;
for (int i = 0; i < size; i += n){
   values[i];
   // do something
)

回答by Buhake Sindi

HashMap.values()doesn't return an ArrayListof values but a ValuesCollection.

HashMap.values()不返回ArrayList值而是一个Values集合。

Source:

来源:

 public Collection<V> values() {
        Collection<V> vs = values;
        return (vs != null ? vs : (values = new Values()));
    }

Valuesis an AbstractCollection. The reason for values is just to reference HashMap's iterator.

Values是一个AbstractCollection。values 的原因只是为了引用 HashMap 的迭代器。

Your question:

你的问题:

Question is how much it costs to create an ArrayList from a HashMap.values() Collection?

问题是从 HashMap.values() 集合创建一个 ArrayList 需要多少成本?

That's a linear complexity (as Bozho said) since

这是一个线性复杂性(如 Bozho 所说),因为

ArrayList<V> valuesList = new ArrayList<V>(hashMap.values());

the ArrayList, valuesListcalls the collection hashMaptoArray()method which essentially does a forloop from 0..N (size) element in the collection.

ArrayListvaluesList调用集合hashMaptoArray()方法,该方法基本上for从集合中的0..N(大小)元素执行循环。

Hope this helps.

希望这可以帮助。

回答by Peter Lawrey

To elaborate on @Bozho's solution you can just do.

要详细说明@Bozho 的解决方案,您可以这样做。

int count = 0;
for(Value value: map.values())
   if(count++ % 5 == 0)
     // do something.