当前位置: 首页 > news >正文

Java 中 List 的 7 种遍历方式 及 性能对比

# for i 循环
for (int i = 0; i < list.size(); i++) {list.get(i);
}# 增强for循环
for (int item : list) {
}# iterator for 循环
for (Iterator<Integer> iterator = list.iterator(); iterator.hasNext(); ) {iterator.next();
}# iterator while 循环
Iterator<Integer> iterator = list.iterator();
while (iterator.hasNext()) {iterator.next();
}# list.forEach 循环
list.forEach(item -> {
});# list.stream().forEach 循环
list.stream().forEach(item -> {
});# list.parallelStream().forEach 循环list.parallelStream().forEach(item -> {});

性能比较

每种循环各遍历10000_0000次 total cost time : 699 ms
for i 循环                                 : 63 ms, 9.01%
iterator for 循环                          : 89 ms, 12.73%
iterator while 循环                        : 86 ms, 12.30%
for Boost 循环                             : 161 ms, 23.03%
list.forEach 循环                          : 115 ms, 16.45%
list.stream().forEach 循环                 : 115 ms, 16.45%
list.parallelStream().forEach 循环         : 66 ms, 9.44%
http://www.lryc.cn/news/160477.html

相关文章:

  • 【Github】git本地仓库建立与远程连接
  • 【瑞萨零基础入门】瑞萨MCU零基础入门系列教程(更新连载中)
  • Bean 的生命周期总结
  • 【Python】环境的搭建
  • 2021 ICPC 昆明 I Mr Main and Windmills(直线与线段的交点)
  • SpringCloudAlibaba Gateway(一)简单集成
  • 逻辑回归(Logistic Regression)
  • Leetcode129. 求根到叶子节点数字之和
  • 0401hive入门-hadoop-大数据学习.md
  • springboot项目打包优化,将所有第三方包单独打包至lib目录
  • 使用 Ccrypt 在 Linux 中加密/解密文件
  • poi3.10 excel xls 设置列宽行高背景色加粗
  • 揭秘分布式文件系统大规模元数据管理机制——以Alluxio文件系统为例
  • 微信小程序onReachBottom事件使用
  • 数据孤岛的突破口在哪里?
  • 【送书活动】全网超50万粉丝的Linux大咖良许,出书了!
  • 深入浅出学Verilog--基础语法
  • 基于Spring、SpringMVC、Mybatis的超市管理系统
  • spring中的@Configuration配置类和@Component
  • 企业架构LNMP学习笔记29
  • Ubuntu14.04离线安装gcc-5.3.0
  • axios返回几种数据格式? 其中Blob返回时的size是什么意思?
  • 【GO语言基础】基本数据类型
  • 【Python】OpenCV立体相机配准与三角化代码实现
  • 通过Idea或命令将本地项目上传至git
  • python selenium 爬虫教程
  • Linux基础知识及常见指令
  • 分享一个基于Python和Django的产品销售收入数据分析系统源码
  • UniTask保姆级教程
  • uni-app 可视化创建的项目 移动端安装调试插件vconsole