Java集合的Stack、Queue、Map的遍历

来源:互联网 发布:u盘禁止写入软件 编辑:程序博客网 时间:2024/05/17 03:01

一、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<String, String> map = new HashMap<String, String>(); 
                map.put("1" , "a" ); 
                map.put("2" , "b" ); 
                map.put("3" , "c" ); 

                //最简洁、最通用的遍历方式 
                for (Map.Entry<String, String> entry : map.entrySet()) { 
                        System.out.println(entry.getKey() + " = " + entry.getValue()); 
                } 
                //Java5之前的比较简洁的便利方式1 
                System.out.println("----1----" ); 
                for (Iterator<Map.Entry<String, String>> it = map.entrySet().iterator(); it.hasNext();) { 
                        Map.Entry<String, String> entry = it.next(); 
                        System.out.println(entry.getKey() + " = " + entry.getValue()); 
                } 
                //Java5之前的比较简洁的便利方式2 
                System.out.println("----2----" ); 
                for (Iterator<String> it = map.keySet().iterator(); it.hasNext();) { 
                        String key = it.next(); 
                        System.out.println(key + " = " + map.get(key)); 
                } 
        } 
}
 
3 = c 
2 = b 
1 = a 
----1---- 
3 = c 
2 = b 
1 = a 
----2---- 
3 = c 
2 = b 
1 = a 

Process finished with exit code 0
 
二、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<Integer> q = new LinkedBlockingQueue<Integer>(); 
                //初始化队列 
                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()); 
                } 
        } 
}
 
-------1----- 





-------2----- 






Process finished with exit code 0
 
三、Stack的遍历
 
import java.util.Stack; 

/** 
* 栈的遍历 

* @author leizhimin 2009-7-22 14:55:20 
*/
 
public class TestStack { 
        public static void main(String[] args) { 
                Stack<Integer> s = new Stack<Integer>(); 
                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()); 
//                } 
        } 
}
 





------1----- 





------2----- 

Process finished with exit code 0
 
 
在遍历集合时候,优先考虑使用foreach语句来做,这样代码更简洁些。

来源:http://blog.sina.com.cn/s/blog_7d11392b0100y2e9.html

原创粉丝点击