栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Collection集合和Map集合循环遍历三种方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Collection集合和Map集合循环遍历三种方法

Collection集合的四种遍历方式:

1、迭代器

    public static void main(String[] args) {
        List list = new ArrayList<>();
        Collections.addAll(list,"小明","小红","小芳","小雅");
        Iterator iterator = list.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

2、增强for循环

    public static void main(String[] args) {
        List list = new ArrayList<>();
        Collections.addAll(list,"小明","小红","小芳","小雅");
        for (String item : list) {
            System.out.println(item);
        }
    }

3、foreach循环和Lambda表达式

   public static void main(String[] args) {
        List list = new ArrayList<>();
        Collections.addAll(list,"小明","小红","小芳","小雅");
        list.forEach(new Consumer() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });
    }
    // 使用Lambda表达式简化
    public static void main(String[] args) {
        List list = new ArrayList<>();
        Collections.addAll(list,"小明","小红","小芳","小雅");
        list.forEach(System.out::println);
    }

4、List集合是有索引的,而Set集合无索引,因此List集合比Set集合多了一个fori循环

   public static void main(String[] args) {
        List list = new ArrayList<>();
        Collections.addAll(list,"小明","小红","小芳","小雅");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }
Map集合遍历的三种 遍历方式

1、由键找值

    public static void main(String[] args) {
        Map hashMap = new HashMap<>();
        hashMap.put("面",2);
        hashMap.put("水",3);
        hashMap.put("鱼",4);
        hashMap.put("龟",5);
        Set keySet = hashMap.keySet();
        for (String key : keySet) {
            System.out.println(key+hashMap.get(key));
        }
    }

2、键值对

    public static void main(String[] args) {
        Map hashMap = new HashMap<>();
        hashMap.put("面",2);
        hashMap.put("水",3);
        hashMap.put("鱼",4);
        hashMap.put("龟",5);
        Set> entries = hashMap.entrySet();
        for (Map.Entry entry : entries) {
            System.out.println(entry.getKey()+entry.getValue());
        }
    }

3、forEach循环以及使用Lambda表达式简化

    public static void main(String[] args) {
        Map hashMap = new HashMap<>();
        hashMap.put("面",2);
        hashMap.put("水",3);
        hashMap.put("鱼",4);
        hashMap.put("龟",5);
        hashMap.forEach(new BiConsumer() {
            @Override
            public void accept(String s, Integer integer) {
                System.out.println(s+integer);
            }
        });
    }
    // 使用Lambda表达式简化
    public static void main(String[] args) {
        Map hashMap = new HashMap<>();
        hashMap.put("面",2);
        hashMap.put("水",3);
        hashMap.put("鱼",4);
        hashMap.put("龟",5);
        hashMap.forEach((s, integer) -> System.out.println(s+integer));
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/716761.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号