首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >迭代fastutil原语ArrayLists的最佳实践,例如IntArrayList?

迭代fastutil原语ArrayLists的最佳实践,例如IntArrayList?
EN

Stack Overflow用户
提问于 2020-05-18 01:05:11
回答 1查看 169关注 0票数 0

在java语言中,从fastutil library读取IntArrayList的最好方法是什么?

fastutil是一个用于提高性能的库,其性能高于标准库的集合类和算法。因此,在这种情况下,“最佳”意味着最好的性能。

我遵循advice in the fastutil docs并将我的集成开发环境(Eclipse)设置为在装箱发生时发出警告: Window ->首选项-> Java ->编译器->错误/警告->潜在编程问题->装箱和取消装箱转换->设置为警告

但是,Eclipse似乎省略了一些警告。有关详细信息,请参阅下面代码中的内容。

到目前为止,我遇到了以下用于迭代的替代方案:

代码语言:javascript
复制
IntArrayList list = ...;

// 1. for-loop-with-index-variable
for (int i = 0; i < list.size(); i++) {
    // do something with list.getInt(i)
}

// 2. for-each-loop-with-wrapped
Integer value = list.getInt(0); // Eclipse correctly warns in this line
for (Integer element : list) { // autoboxing happens here somewhere, but Eclipse does NOT warn
    // do something with element
}

// 3. for-each-loop-with-primitive
for (int element : list) { // Eclipse does NOT warn. Does autoboxing happen here?
    // do something with element
}

// 4. forEach-method-with-consumer
list.forEach((int element) -> {
    // do something with element
});

// 5. for-loop-with-IntIterator
for (IntIterator iter = list.iterator(); iter.hasNext();) {
    // do something with iter.nextInt()
}
EN

回答 1

Stack Overflow用户

发布于 2020-05-28 04:05:50

绝对是#1. :)

我必须添加更多的字符StackOverflow不会让我张贴这个。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61855626

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档