Java for循環對集合的遍歷

java集合類的使用能夠說是無處不在,總的咱們能夠將之分爲三大塊,分別是從Collection接口延伸出的List、Set和以鍵值對形式做存儲的Map類型集合。java

     許多狀況須要咱們遍歷出集合中的元素,並作相應的處理。
     下面對各類類型的集合的遍歷作一些總結,關於加強for循環,須要注意的是,使用加強for循環沒法訪問數組下標值,對於集合的遍歷其內部採用的也是Iterator的相關方法。若是隻作簡單遍歷讀取,加強for循環確實減輕很多的 代碼量。
關於List與Set類型集合的遍歷:
[java] 
import java.util.ArrayList;  
import java.util.HashSet;  
import java.util.Iterator;  
import java.util.List;  
import java.util.Set;  
  
public class ListAndSetTest {  
  
    public static void main(String[] args) {  
        // List集合的遍歷  
        listTest();  
        // Set集合的遍歷  
        setTest();  
    }  
  
    private static void setTest() {  
        Set<String> set = new HashSet<String>();  
        set.add("JAVA");  
        set.add("C");  
        set.add("C++");  
        // 重複的加不進去。  
        set.add("JAVA");  
        set.add("JAVASCRIPT");  
  
        //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("java111");  
        list.add("java222");  
        list.add("java333");  
        list.add("java444");  
        list.add("java555");  
  
        // 遍歷方式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()方法:
[java] view plaincopy
//加強For循環  
public class MapTest {  
  
    public static void main(String[] args) {  
        // 建立一個HashMap對象,並加入了一些鍵值對。  
        Map<String, String> maps = new HashMap<String, String>();  
        maps.put("111", "java111");  
        maps.put("222", "java222");  
        maps.put("333", "java333");  
        maps.put("444", "java444");  
        maps.put("555", "java555");  
          
        // 傳統的遍歷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);  
        }  
    }  
}  
相關文章
相關標籤/搜索