Java集合的Stack、Queue、Map的遍历
在集合操作中,常常离不开对集合的遍历,对集合遍历一般来说一个foreach就搞定了,但是,对于Stack、Queue、Map类型的遍历,还是有一些讲究的。
下面是常用的写法:
一、Map的遍历
- importjava.util.HashMap;
- importjava.util.Iterator;
- importjava.util.Map;
- /**
- *Map的遍历,这个遍历比较特殊,有技巧
- *
- *@authorleizhimin2009-7-2215:15:34
- */
- publicclassTestMap{
- publicstaticvoidmain(String[]args){
- Map<String,String>map=newHashMap<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();){
- Stringkey=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
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的遍历
- importjava.util.Queue;
- importjava.util.concurrent.LinkedBlockingQueue;
- /**
- *队列的遍历
- *
- *@authorleizhimin2009-7-2215:05:14
- */
- publicclassTestQueue{
- publicstaticvoidmain(String[]args){
- Queue<Integer>q=newLinkedBlockingQueue<Integer>();
- //初始化队列
- for(inti=0;i<5;i++){
- q.offer(i);
- }
- System.out.println("-------1-----");
- //集合方式遍历,元素不会被移除
- for(Integerx:q){
- System.out.println(x);
- }
- System.out.println("-------2-----");
- //队列方式遍历,元素逐个被移除
- while(q.peek()!=null){
- System.out.println(q.poll());
- }
- }
- }
三、Stack的遍历
- importjava.util.Stack;
- /**
- *栈的遍历
- *
- *@authorleizhimin2009-7-2214:55:20
- */
- publicclassTestStack{
- publicstaticvoidmain(String[]args){
- Stack<Integer>s=newStack<Integer>();
- for(inti=0;i<10;i++){
- s.push(i);
- }
- //集合遍历方式
- for(Integerx: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(Integerx:s){
- //System.out.println(s.pop());
- //}
- }
- }
并且推荐使用foreach