遍历Collection集合中的6种方法:

下面的代码演示了遍历Collection集合的6种方法,注意Collection集合的遍历远不止于增强for循环,和迭代器两种。

代码如下:

  1 package com.qls.traverse;
  2 
  3 import java.util.ArrayList;
  4 import java.util.Arrays;
  5 import java.util.Collections;
  6 import java.util.Enumeration;
  7 import java.util.Iterator;
  8 import java.util.LinkedList;
  9 import java.util.List;
 10 import java.util.Stack;
 11 
 12 /**
 13  * 下面是遍历Collection的几种方法,以List接口为例:
 14  * @author 秦林森
 15  *
 16  */
 17 public class ListTest {
 18 
 19     public static void main(String[] args) {
 20         // TODO Auto-generated method stub
 21         String[] s="sixi is one of the most beautiful villages in china".split(" ");
 22         List<String> list = Arrays.asList(s);
 23         /**
 24          * 第一种方法用增强for循环。(这里List之所以能用增强for循环其原因在于它实现了Iterable接口)
 25          */
 26         for(String str:list){
 27             System.out.print(str+" ");
 28         }
 29         System.out.println();
 30         System.out.println("************");
 31         /**
 32          * 第二种方法用Iterator
 33          */
 34         Iterator<String> it = list.iterator();
 35         while(it.hasNext()){
 36             String next = it.next();
 37             System.out.print(next+" ");
 38         }
 39         System.out.println();
 40         System.out.println("************");
 41         /**
 42          * 第三种方法主要针对LinkedList。因为LinkedList 既有栈(stack)的特点,又有队列(Queue)
 43          * 的特点。所以遍历LinkedList中的元素。根据stack和queue,可以进行相关的遍历。
 44          * 遍历的方法如下所示:
 45          */
 46         //Using linkedList as a stack
 47         LinkedList<String> list2=new LinkedList<>(list);//创建一个LinkeList包含list中的全部元素。
 48         while(!list2.isEmpty()){
 49             System.out.print(list2.removeFirst()+" ");
 50         }
 51         System.out.println();
 52         System.out.println("************");
 53         /**
 54          * Using linkedList as a queue
 55          */
 56         LinkedList<String> list3=new LinkedList<>(list);
 57         while(list3.peek() != null){
 58             System.out.print(list3.poll()+" ");
 59         }
 60         System.out.println();
 61         System.out.println("************");
 62         /**
 63          * 第四种方法把所有的Collection都可以当做Enumeration进行遍历
 64          * Collections.enumeration(c)
 65          */
 66         ArrayList<String> list4=new ArrayList<>(list);
 67         Enumeration<String> e = Collections.enumeration(list4);
 68         while(e.hasMoreElements()){
 69             System.out.print(e.nextElement()+" ");
 70         }
 71         /**第五种方法
 72          * 当然还有其他方法如:
 73          */
 74         System.out.println();
 75         System.out.println("************");
 76         for(int i=0;i<list4.size();i++){
 77             System.out.print(list4.get(i)+" ");
 78         }
 79         System.out.println();
 80         System.out.println("************");
 81         /**第六种方法:
 82          *再如:
 83          */
 84         while(!list4.isEmpty()){
 85             int index=0;
 86             System.out.print(    list4.remove(index++)+" ");
 87         }
 88         /**
 89          * 备注:在List接口中的所有实现类中最常用的是ArrayList   LinkedList  
 90          * ArraList比LinkedList的速度快,一般情况下选中ArrayList的情况比LinkedList多。
 91          * 在ArrayList源码中有一个serialVersionUID,这个数字保证了,
 92          * 写入文件(ObjectOutputStream.writeObject(Object))
 93          * 读取文件(ObjectInputStream.readObject())可以顺利进行,
 94          * 并且指明这个数字,可以保持各个版本的兼容性。有利于文件传输。
 95          */
 96         
 97     }
 98 
 99 }/*Output:
100 sixi is one of the most beautiful villages in china 
101 ************
102 sixi is one of the most beautiful villages in china 
103 ************
104 sixi is one of the most beautiful villages in china 
105 ************
106 sixi is one of the most beautiful villages in china 
107 ************
108 sixi is one of the most beautiful villages in china 
109 ************
110 sixi is one of the most beautiful villages in china 
111 ************
112 sixi is one of the most beautiful villages in china *///:~
原文地址:https://www.cnblogs.com/1540340840qls/p/6220730.html