我是靠谱客的博主 炙热芹菜,最近开发中收集的这篇文章主要介绍【Java】for、forEach,Iterator在ArrayList、LinkedList上的性能比较前言一、forEach与Iterator二、测试三、总结,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

面对ArrayList以及LinkedList的循环遍历,使用forEach、for,Iterator谁的效率会好一些呢?

文章目录

  • 前言
  • 一、forEach与Iterator
    • 1.发行时间
    • 2.区别
  • 二、测试
  • 三、总结


前言

日常生活中,循环遍历是少不了的,那么对于Java的几种遍历方式都很熟悉嘛?

  • 在刚接触Java的时候我们知道循环分为 for 循环、 while以及 do while循环,那么除了这三种遍历还有别的方式嘛?
  • 没错,还有forEach,慢慢地学习中我们发现,jdk1.5版本之后更新的forEach太好用了!比for、while,do while书写方面快多了!
  • 那么在效率上,哪个好点呢?
  • 本文简单的从ArrayList,LinkedList角度进行测试。

提示:这里单纯说明测试结果以及对于一些使用方式的比较说明,对于java循环讲解暂时略过

一、forEach与Iterator

1.发行时间

forEach是jdk5(jdk1.5)版本之后更新产物;Iterator则是jdk8(jdk1.8)的产物,Iterator是collection 的一个迭代器。

2.区别

  1. forEach通常用于一次性遍历整个集合,大大提升了代码的简洁性和可阅读性,但期间无法控制遍历的过程。而Iterator通过hasNext(),next() 更好地控制便利过程的每一步。
  2. forEach在遍历过程中严禁改变集合的长度,且无法对集合的删除或添加等操作。而Iterator可以在遍历过程中对集合元素进行删除操作。
  3. 在设计模式中迭代器模式搭配组合模式一起使用,可以递归方式控制树状结构的遍历。
  4. Map虽然不能使用forEach,但可以通过Map.Entry来对Map进行forEach遍历
    for(Map.Entry entry :map.entrySet()){
		  // code...
    }

二、测试

测试须知

  • 测试容器:本次是对ArrayList以及LinkedList的循环遍历
  • 测试项:forEach、for,Iterator
  • 赋值量:
    • 100000条长度为10的字符串
    • 10000000条长度为10的字符串
    • 1000条长度为10的字符串
  • 运算时间方法: 使用的是 System.nanoTime()

并不绝对,测试环境也要考虑

结果普遍如下

································here for loop······················ArrayList····················
execution time:   0.6844 ms.
································here forEach loop··················ArrayList····················
execution time:   1.9834 ms.
································here iterator loop·················ArrayList····················
execution time:   0.8786 ms.
································································································
·····After many tests, in most cases, the for the most efficiency, followed by the iterator.····
································································································
································here for loop·····················LinkedList····················
execution time:3035.3940 ms.
································here forEach loop·················LinkedList····················
execution time:   1.4215 ms.
································here iterator loop················LinkedList····················
execution time:   0.8353 ms.

↑ 在100000条数据测试中,for对于ArrayList的遍历效率略胜一筹,与iterator差不多;对于LinkedList,for效率最差,iterator最高。

································here for loop······················ArrayList····················
execution time:  56.7496 ms.
································here forEach loop··················ArrayList····················
execution time:  83.0643 ms.
································here iterator loop·················ArrayList····················
execution time:  58.5370 ms.
································································································
·····After many tests, in most cases, the for the most efficiency, followed by the iterator.····
································································································
································here for loop·····················LinkedList····················
pass for loop
execution time:   0.0060 ms.
································here forEach loop·················LinkedList····················
execution time: 694.5913 ms.
································here iterator loop················LinkedList····················
execution time: 648.3043 ms.

↑ 在10000000条数据测试中,for对于ArrayList的遍历效率略胜一筹,与iterator差不多;对于LinkedList,for效率最差直接卡死,iterator略高。

································here for loop······················ArrayList····················
execution time:   0.0275 ms.
································here forEach loop··················ArrayList····················
execution time:   0.1711 ms.
································here iterator loop·················ArrayList····················
execution time:   0.0382 ms.
································································································
·····After many tests, in most cases, the for the most efficiency, followed by the iterator.····
································································································
································here for loop·····················LinkedList····················
execution time:   1.0022 ms.
································here forEach loop·················LinkedList····················
execution time:   0.2920 ms.
································here iterator loop················LinkedList····················
execution time:   0.0428 ms.

↑ 在1000条数据测试中,for对于ArrayList的遍历效率略胜一筹,与iterator差不多;对于LinkedList,for效率最差,iterator最高。

三、总结

经过上述实验,ArrayList推荐 for , iteratorLinkedList 推荐 iterator,for循环会很慢。

最后

以上就是炙热芹菜为你收集整理的【Java】for、forEach,Iterator在ArrayList、LinkedList上的性能比较前言一、forEach与Iterator二、测试三、总结的全部内容,希望文章能够帮你解决【Java】for、forEach,Iterator在ArrayList、LinkedList上的性能比较前言一、forEach与Iterator二、测试三、总结所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(77)

评论列表共有 0 条评论

立即
投稿
返回
顶部