java集合類可分為三大塊,分別是從Collection接口延伸出的List、Set和以鍵值對形式作存儲的Map類型集合。 關于增強for循環,需要注意的是,使用增強for循環無法訪問數組下標值,對于集合的遍歷其內部采用的也是Iterator的相關方法。如果只做簡單遍歷讀取,增強for循環確實減輕不少的代碼量。
集合概念: 1.作用:用于存放對象 2.相當于一個容器,里面包含著一組對象,其中的每個對象作為集合的一個元素出現 3.java的容器有集合類和數組,不同之處是 區別及其常用實現類 List接口: 列表有序 元素可重復 實現類:ArrayList:動態數組列表 LinkedList:雙向鏈表 Set接口: 集無序,元素不可重復 實現類:HashSet:散列集 TreeSet:樹集 內部排序 Map接口: 以鍵值對的方式存儲數據 數據-鍵不允許重復 實現類:HashSet:散列集 TreeSet:樹集 內部排序 JDK1.0出現的集合類都是線程安全的,但效率低 JDK1.2出現的集合類都不是線程安全的,但效率高 代碼示例如下:
import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; public class ListAndSet{ public static void main(String[] args) { setTest(); listTest(); } // 遍歷Set集合 PRivate static void setTest() { Set<String> set = new HashSet<String>(); set.add("A"); set.add("B"); set.add("C"); set.add("D"); set.add("E"); //set集合遍歷方法1:使用iterator Iterator<String> it = set.iterator(); while (it.hasNext()) { String value = it.next(); System.out.println(value); } //set集合遍歷方法2:使用增強for循環。 for(String s: set){ System.out.println(s); } } // 遍歷list集合 private static void listTest() { List<String> list = new ArrayList<String>(); list.add("111"); list.add("222"); list.add("333"); list.add("444"); list.add("555"); // 遍歷方式1:使用iterator Iterator<String> it = list.iterator(); while (it.hasNext()) { String value = it.next(); System.out.println(value); } // 遍歷方法2:使用傳統for循環進行遍歷。 for (int i = 0, size = list.size(); i < size; i++) { String value = list.get(i); System.out.println(value); } // 遍歷方法3:使用增強for循環進行遍歷。 for (String value : list) { System.out.println(value); } } } //關于Map類型集合的遍歷,keySet()與entrySet()方法//增強For循環 public class Map{ public static void main(String[] args) { // 創建一個HashMap對象,并加入了一些鍵值對。 Map<String, String> maps = new HashMap<String, String>(); maps.put("111", "111"); maps.put("222", "222"); maps.put("333", "333"); maps.put("444", "444"); maps.put("555", "555"); // 傳統的遍歷map集合的方法1; keySet() //traditionalMethod1(maps); // 傳統的遍歷map集合的方法2; entrySet() //traditionalMethod2(maps); // 使用增強For循環來遍歷map集合方法1; keySet() //strongForMethod1(maps); // 使用增強For循環來遍歷map集合方法2; entrySet() strongForMethod2(maps); } private static void strongForMethod2(Map<String, String> maps) { Set<Entry<String, String>> set = maps.entrySet(); for (Entry<String, String> entry : set) { String key = entry.getKey(); String value = entry.getValue(); System.out.println(key + " : " + value); } } private static void strongForMethod1(Map<String, String> maps) { Set<String> set = maps.keySet(); for (String s : set) { String key = s; String value = maps.get(s); System.out.println(key + " : " + value); } } // 使用entrySet()方法,獲取maps集合中的每一個鍵值對, private static void traditionalMethod2(Map<String, String> maps) { Set<Map.Entry<String, String>> sets = maps.entrySet(); // 取得迭代器遍歷出對應的值。 Iterator<Entry<String, String>> it = sets.iterator(); while (it.hasNext()) { Map.Entry<String, String> entry = (Entry<String, String>) it.next(); String key = entry.getKey(); String value = entry.getValue(); System.out.println(key + " : " + value); } } // 使用keySet()方法,獲取maps集合中的所有鍵,遍歷鍵取得所對應的值。 private static void traditionalMethod1(Map<String, String> maps) { Set<String> sets = maps.keySet(); // 取得迭代器遍歷出對應的值。 Iterator<String> it = sets.iterator(); while (it.hasNext()) { String key = it.next(); String value = maps.get(key); System.out.println(key + " : " + value); } } }新聞熱點
疑難解答