java数组、容器遍历方式对比,for i、for each、Iterator

java数组、容器遍历方式对比,for i、for each、Iterator

博客地址

源码地址

1. 先上结论

容器for ifor eachIterator
array
List
Set
Map
  • for each支持所有容器,只能遍历全部。优先使用。
  • List支持所有遍历方式,array不支持Iterator(能不用则不用),Set、Map不支持for i
  • Map优先使用EntrySet方式遍历,因为:
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fuEfhj4Q-1653414959238)(./assets/README-1653303112914.png)]

2. 测试代码

/** 结论见 README.MD */
public class LoopTest {
    private static String[] array = new String[]{"array111", "array222"};
    private static List<String> list = CollUtil.list(false, "list111", "list222");
    private static Set<String> set = CollUtil.set(false, "set111", "set222");
    private static Map<String, String> map = MapUtil.builder("key111", "val111").put("key222", "val222").build();

    public static void main(String[] args) {

        // 1. array
        for (int i = 0; i < array.length; i++) {
            System.out.println("arrayForI:" + array[i]);
        }
        for (String value : array) {
            System.out.println("arrayForEach:" + value);
        }

        // 2. list
        for (int i = 0; i < list.size(); i++) {
            System.out.println("listFori:" + list.get(i));
        }
        for (String value : list) {
            System.out.println("listForEach:" + value);
        }
        //美[ˈɪtəreɪt]
        Iterator<String> itList = list.iterator();
        while (itList.hasNext()) {
            String value = itList.next();
            System.out.println("listIterator:" + value);
        }

        // 3. set
        for (String value : set) {
            System.out.println("setForEach:" + value);
        }
        //美[ˈɪtəreɪt]
        Iterator<String> itSet = set.iterator();
        while (itSet.hasNext()) {
            String value = itSet.next();
            System.out.println("setIterator:" + value);
        }

        // 4. map,在急需要优先使用 entrySet,不建议使用
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println("mapForEachEntry:" + entry.getKey() + "==" + entry.getValue());
        }
        for (String key : map.keySet()) {
            System.out.println("mapForEachKey:" + key + "==" + map.get(key));
        }
        for (String value : map.values()) {
            System.out.println("mapForEachVal:" + value);
        }
        //美[ˈɪtəreɪt]
        Iterator<Map.Entry<String, String>> itMap = map.entrySet().iterator();
        while (itMap.hasNext()) {
            Map.Entry entry = itMap.next();
            System.out.println("mapIteratorEntry:" + entry.getKey() + "==" + entry.getValue());
        }
        Iterator<String> itKey = map.keySet().iterator();
        while (itKey.hasNext()) {
            String key = itKey.next();
            System.out.println("mapIteratorKey:" + key + "==" + map.get(key));
        }
        Iterator<String> itVal = map.values().iterator();
        while (itVal.hasNext()) {
            String value = itVal.next();
            System.out.println("mapIteratorVal:" + value);
        }
    }
}

posted on 2022-05-23 18:58  小石头小祖宗  阅读(12)  评论(0编辑  收藏  举报  来源

导航