(转)java中对集合对象list的几种循环访问总结
生活随笔
收集整理的這篇文章主要介紹了
(转)java中对集合对象list的几种循环访问总结
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Java集合的Stack、Queue、Map的遍歷 在集合操作中,常常離不開對集合的遍歷,對集合遍歷一般來說一個foreach就搞定了,但是,對于Stack、Queue、Map類型的遍歷,還是有一些講究的。 最近看了一些代碼,在便利Map時候,慘不忍睹,還有一些是遍歷錯誤,忽略了隊列、棧與普通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<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-----?
0?
1?
2?
3?
4?
-------2-----?
0?
1?
2?
3?
4?
Process finished with exit code 0
Map<String, Integer> map = new HashMap<String, Integer>(); map.put("d", 2); map.put("c", 1); map.put("b", 1); map.put("a", 3);List<Map.Entry<String, Integer>> infoIds =new ArrayList<Map.Entry<String, Integer>>(map.entrySet());//排序前 for (int i = 0; i < infoIds.size(); i++) {String id = infoIds.get(i).toString();System.out.println(id); } //d 2 //c 1 //b 1 //a 3//排序 Collections.sort(infoIds, new Comparator<Map.Entry<String, Integer>>() { public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { //return (o2.getValue() - o1.getValue()); return (o1.getKey()).toString().compareTo(o2.getKey());} }); //排序后 for (int i = 0; i < infoIds.size(); i++) {String id = infoIds.get(i).toString();System.out.println(id); } //根據key排序 //a 3 //b 1 //c 1 //d 2 //根據value排序 //a 3 //d 2 //b 1 //c 1
三、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());?
//????????????????}?
????????}?
} 0?
1?
2?
3?
4?
------1-----?
4?
3?
2?
1?
0?
------2-----?
Process finished with exit code 0 在遍歷集合時候,優先考慮使用foreach語句來做,這樣代碼更簡潔些。
java中對集合對象list的幾種循環訪問的總結如下?
1 經典的for循環?
Java代碼?? public?static?void?main(String[]?args)?{?? ?????????? ????List<String>?list?=?new?ArrayList();?? ????list.add("123");?? ????list.add("java");?? ????list.add("j2ee");?? ????System.out.println("=========經典的for循環=======");?? ????for(int?i=0;?i<list.size();i++){?? ????System.out.println(list.get(i));?? ???}?? }?? 2 增強的for循環?
Java代碼?? public?static?void?main(String[]?args)?{?? ?????????? ????List<String>?list?=?new?ArrayList();?? ????list.add("123");?? ????list.add("java");?? ????list.add("j2ee");?? ????System.out.println("=========Java1.6的for循環=======");?? ????for(String?s:list){?? ????System.out.println(s);?? ????}?? }?? 3 Iterate的使用?
Java代碼?? public?static?void?main(String[]?args)?{?? ?????????? ?????????List<String>?list?=?new?ArrayList();?? ????list.add("123");?? ????list.add("java");?? ????list.add("j2ee");?? ????System.out.println("=========Iterate循環=======");?? ????Iterator<String>?iter?=?list.iterator();?? ????while(iter.hasNext()){?? ????System.out.println(iter.next());?? ????}?? ?????????? } ?
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-----?
0?
1?
2?
3?
4?
-------2-----?
0?
1?
2?
3?
4?
Process finished with exit code 0
HashMap的兩種排序方式
Map<String, Integer> map = new HashMap<String, Integer>(); map.put("d", 2); map.put("c", 1); map.put("b", 1); map.put("a", 3);List<Map.Entry<String, Integer>> infoIds =new ArrayList<Map.Entry<String, Integer>>(map.entrySet());//排序前 for (int i = 0; i < infoIds.size(); i++) {String id = infoIds.get(i).toString();System.out.println(id); } //d 2 //c 1 //b 1 //a 3//排序 Collections.sort(infoIds, new Comparator<Map.Entry<String, Integer>>() { public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { //return (o2.getValue() - o1.getValue()); return (o1.getKey()).toString().compareTo(o2.getKey());} }); //排序后 for (int i = 0; i < infoIds.size(); i++) {String id = infoIds.get(i).toString();System.out.println(id); } //根據key排序 //a 3 //b 1 //c 1 //d 2 //根據value排序 //a 3 //d 2 //b 1 //c 1
三、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());?
//????????????????}?
????????}?
} 0?
1?
2?
3?
4?
------1-----?
4?
3?
2?
1?
0?
------2-----?
Process finished with exit code 0 在遍歷集合時候,優先考慮使用foreach語句來做,這樣代碼更簡潔些。
1 經典的for循環?
Java代碼??
Java代碼??
Java代碼??
?
轉載于:https://www.cnblogs.com/lixuwu/p/5676228.html
總結
以上是生活随笔為你收集整理的(转)java中对集合对象list的几种循环访问总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jQuery 表单选择器
- 下一篇: [原]TCP/UDP使用细节备忘