在平时的开发过程中使用List的场景很多,你知道List的遍历有多少种方式?今天一起来梳理下List的几种遍历方式。这里以java.util.ArrayList为例来演示。
这里有一个最简单的测试类,里边有一个main方法
"prettyprint hljs cpp" deep="5" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">package com.my.template.service;
-
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- /**
- * @date 2022/8/2 18:12
- */
- public class TestList {
- public static void main(String[] args) {
- List
list=new ArrayList(); -
- list.add("hello");
- list.add("ArrayList");
- list.add("!");
- simpleTraverse(list);
- }
- }
这种方式是最简单的,也是最容易想到的。
class="prettyprint hljs cpp" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">/**
- * 最简单的遍历方式
- * @param list
- */
- public static void simpleTraverse(List
list ){ - for(int i=0;i
- System.out.println(list.get(i));
- }
- }
这种方法就是把List当作一个数组,从数组的第一个位置开始循环到数组的最后位置,有以下几点需要注意,
- i的初始值为0,因为数组的第一个下标为0;
- 临界值为list的长度-1,也就是“i
打印结果为:
- <pre class="hljs nginx" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">hello
- ArrayList
- !
这种方式初学者都会的遍历方式,下面看高级点。
2.foreach的遍历方式
上面的遍历方式,下面来看下高级的遍历方法,
class="prettyprint hljs cpp" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">/**
- * 进阶版的遍历方式,foreach
- * @param list
- */
- public static void forEachTraverse(List<String> list){
- for (String str:list) {
- System.out.println(str);
- }
- }
这种方式是利用foreach的用法,很多人不清楚foreach的底层是什么样子的,在idea中找到class文件,看下反编译过来的代码,
从反编译过滤的代码可以看到foreach底层其实是使用的迭代器的方式,也就是下面要说的遍历方式。
3.迭代器的遍历方式
List可以使用迭代器的方式进行遍历是有原因的,因为在list的实现类中均实现了Iterator接口。看下ArrayList中对Iterator接口的实现,
在ArrayList中有静态内部类Itr,该类实现了Iterator接口。同时ArrayList提供了iterator()方法,
这样就可以使用迭代器了。看下迭代器模式的遍历方式,
class="prettyprint hljs dart" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">/**
- * 迭代器的遍历方式
- * @param list
- */
- public static void iteratorTraverse(List<String> list){
- Iterator<String> iterator=list.iterator();
- while(iterator.hasNext()){
- System.out.println(iterator.next());
- }
- }
看下是不是也是很简单。下面看最后一种遍历方式,流式遍历。
4.流式的遍历方式
所谓流式的遍历,是java8提供的最新的方式,
class="prettyprint hljs php" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">/**
- * 流式遍历
- * @param list
- */
- public static void streamTraverse(List<String> list){
- list.stream().forEach(str->{
- System.out.println(str);
- });
- }
是不是很简单,对于stream()的API后边会专门分析,这里知道这种遍历方式即可,有兴趣的可以先看看该种方式的实现。
总结,本文主要梳理里在日常的开发过程中对List的遍历方式,没有最好的方式只有适合自己的。