Collectionを巡る


詳細
一、Mapの遍歴
import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;

/**
 * Map   ,        ,   
 *
 * @author leizhimin 2009-7-22 15:15:34
 */
 public class TestMap {
         public static void main(String[] args) {
                 Map map = new HashMap();
                 map.put("1", "a");
                 map.put("2", "b");
                 map.put("3", "c");

                //   、        
                for (Map.Entry entry : map.entrySet()) {
                         System.out.println(entry.getKey() + " = " + entry.getValue());
                 }
                 //Java5            1
                System.out.println("----1----");
                 for (Iterator> it = map.entrySet().iterator(); it.hasNext();) {
                         Map.Entry entry = it.next();
                         System.out.println(entry.getKey() + " = " + entry.getValue());
                 }
                 //Java5            2
                System.out.println("----2----");
                 for (Iterator it = map.keySet().iterator(); it.hasNext();) {
                         String key = it.next();
                         System.out.println(key + " = " + map.get(key));
                 }
         }
 }

二、Queueの遍歴
import java.util.Queue; 
import java.util.concurrent.LinkedBlockingQueue; 

/** 
*       
* 
* @author leizhimin 2009-7-22 15:05:14 
*/ 
public class TestQueue { 
        public static void main(String[] args) { 
                Queue q = new LinkedBlockingQueue(); 
                //      
                for (int i = 0; i < 5; i++) { 
                        q.offer(i); 
                } 
                System.out.println("-------1-----"); 
                //      ,        
                for (Integer x : q) { 
                        System.out.println(x); 
                } 
                System.out.println("-------2-----"); 
                //      ,        
                while (q.peek() != null) { 
                        System.out.println(q.poll()); 
                } 
        } 
}

三、Stackの遍歴
import java.util.Stack;

/**
 *     
 *
 * @author leizhimin 2009-7-22 14:55:20
 */
 public class TestStack {
         public static void main(String[] args) {
                 Stack s = new Stack();
                 for (int i = 0; i < 10; i++) {
                         s.push(i);
                 }
                 //      
                 for (Integer x : s) {
                         System.out.println(x);
                 }
                 System.out.println("------1-----");
                 //       
 //                while (s.peek()!=null) {     //        ,     ,        
                 while (!s.empty()) {
                         System.out.println(s.pop());
                 }
                 System.out.println("------2-----");
                 //       
//                for (Integer x : s) {
 //                        System.out.println(s.pop());
 //                }
         }
 }

四、ArrayList遍歴の4つの方法
package com.test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ArrayListDemo {
    public static void main(String args[]){
        List list = new ArrayList();
        list.add("luojiahui");
        list.add("luojiafeng");
        //  1
        Iterator it1 = list.iterator();
        while(it1.hasNext()){
            System.out.println(it1.next());
        }
        //  2  
        for(Iterator it2 = list.iterator();it2.hasNext();){
             System.out.println(it2.next());
        }
        //  3
        for(String tmp:list){
            System.out.println(tmp);
        }
        //  4
        for(int i = 0;i < list.size(); i ++){
            System.out.println(list.get(i));
        }
    }
}

コレクションを巡るときはforeach文を優先して、コードをより簡潔にします.