数组array、容器(List、Set、Map)的遍历方式总结。补充:使用 entrySet 遍历 Map 类集合 KV,而不是 keySet 方式进行遍历

  array List Set Map 优缺点 备注
fori     按下标遍历,灵活 推荐
foreach 遍历全部,简洁 推荐,全都支持,优先使用
Iterator   代码罗嗦 不推荐

 

demo如下

 

package top.lishuoboy.java_basic.loop;

import java.util.*;

/**
 * 数组遍历方式方法总结
 * 各种循环,for循环总结
 * tag:for循环、迭代器循环,map的各种循环
 */
public class LoopTest {

    private static String[] array = new String[]{"array11", "array22"};
    private static List list = new ArrayList();
    private static Set set = new HashSet();
    private static Map map = new HashMap();

    static {
        list.add("list11");
        list.add("list22");

        set.add("set11");
        set.add("set22");

        map.put("key1", "map11");
        map.put("key2", "map22");

    }

    public static void main(String[] args) {
        arrFori();
        arrForeach();


        listFori();
        listForeach();
        listIterator();


        setForeach();
        setIterator();


        mapForeachEntry();
        mapIteratorEntry();
        mapForeachKeyValue();
        mapIteratorKeyValue();
        mapForeachKey();
        mapIteratorKey();
        mapForeachValue();
        mapIteratorValue();

    }


    private static void arrFori() {
        for (int i = 0; i < array.length; i++) {
            System.out.println("fori:" + array[i]);
        }
    }


    private static void arrForeach() {
        for (String value : array) {
            System.out.println("foreach:" + value);
        }
    }


    private static void listFori() {
        for (int i = 0; i < list.size(); i++) {
            System.out.println("listFori:" + list.get(i));
        }
    }


    private static void listForeach() {
        for (String value : list) {
            System.out.println("listForeach:" + value);
        }
    }


    private static void listIterator() {
        //美[ˈɪtəreɪt]
        Iterator it = list.iterator();
        while (it.hasNext()) {
            String value = it.next();
            System.out.println("listIterator:" + value);
        }
    }


    private static void setForeach() {
        for (String value : set) {
            System.out.println("setForeach:" + value);
        }
    }


    private static void setIterator() {
        //美[ˈɪtəreɪt]
        Iterator it = set.iterator();
        while (it.hasNext()) {
            String value = it.next();
            System.out.println("setIterator:" + value);
        }
    }


    /**
     * 1.1 Entry方式,强力推荐
     */
    private static void mapForeachEntry() {
        for (Map.Entry entry : map.entrySet()) {
            System.out.println("mapForeachEntry:" + entry.getKey() + "@" + entry.getValue());
        }
    }

    /**
     * 1.2 Entry方式,可用,比上面的代码罗嗦
     */
    private static void mapIteratorEntry() {
        //美[ˈɪtəreɪt]
        Iterator> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = it.next();
            System.out.println("mapIteratorEntry:" + entry.getKey() + "@" + entry.getValue());
        }
    }


    /**
     * 2.1 key获取value方式,效率低,强力不推荐
     */
    private static void mapForeachKeyValue() {
        for (Map.Entry entry : map.entrySet()) {
            System.out.println("mapForeachKeyValue:" + entry.getKey() + "@" + entry.getValue());
        }
    }

    /**
     * 2.2 key获取value方式,效率低,强力不推荐,比上面的代码罗嗦
     */
    private static void mapIteratorKeyValue() {
        //美[ˈɪtəreɪt]
        Iterator> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = it.next();
            System.out.println("mapIteratorEntry:" + entry.getKey() + "@" + entry.getValue());
        }
    }


    /**
     * 3.1 只要key,意义不大
     */
    private static void mapForeachKey() {
        for (String key : map.keySet()) {
            System.out.println("mapForeachKey:" + key);
        }
    }


    /**
     * 3.2 只要key,意义不大
     */
    private static void mapIteratorKey() {
        //美[ˈɪtəreɪt]
        Iterator it = map.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            System.out.println("mapIteratorKey:" + key);
        }
    }

    /**
     * 4.1 只要value,意义不大
     */
    private static void mapForeachValue() {
        for (String value : map.values()) {
            System.out.println("mapForeachKey:" + value);
        }
    }


    /**
     * 4.2 只要value,意义不大
     */
    private static void mapIteratorValue() {
        //美[ˈɪtəreɪt]
        Iterator it = map.values().iterator();
        while (it.hasNext()) {
            String value = it.next();
            System.out.println("mapIteratorValue:" + value);
        }
    }


}

 

 

补充说明一下map使用,如下图

数组array、容器(List、Set、Map)的遍历方式总结。补充:使用 entrySet 遍历 Map 类集合 KV,而不是 keySet 方式进行遍历_第1张图片 

你可能感兴趣的:(java)