zoukankan      html  css  js  c++  java
  • Java遍历List、Map的集合方法

    使用JDK8新特性,for循环增强,Iterator,while做遍历List集合

        public static void main(String[] args) {
    
            List<String> list = new ArrayList<>();
            list.add("test1x");
            list.add("test2x");
            list.add("test3x");
            System.out.println(list);
    
            System.out.println("===========");
            list.forEach((v)->{
                System.out.println(v);
            });
    
            System.out.println("===========");
            for(String str : list) {
                System.out.println(str);
            }
    
            System.out.println("===========");
            for(int i = 0; i < list.size();i++) {
                System.out.println(list.get(i));
            }
    
            System.out.println("===========");
            Iterator<String> it = list.iterator();
            while(it.hasNext()) {
                System.out.println(it.next());
            }
    
            System.out.println("=========");
            int i = 0;
            while(i < list.size()){
                System.out.println(list.get(i));
                i++;
            }
    
        }
    

    使用JDK8新特性,for循环增强,Iterator,Map.Entry<K,V>做遍历Map集合

    public static void main(String[] args) {
    
            Map<Integer,String> map = new HashMap<>();
            map.put(1,"test1x");
            map.put(2,"test2x");
            map.put(3,"test3x");
            System.out.println(map);
    
            System.out.println("==========");
            map.forEach((k ,v) -> {
                    System.out.println(k + " , " + v);
            });
    
            System.out.println("========================");
            for(Map.Entry<Integer,String> entry : map.entrySet()) {
                System.out.println(entry.getKey() + " , " + entry.getValue());
            }
    
            System.out.println("==========");
            for(int key : map.keySet()) {
                System.out.println(key + " , " + map.get(key));
            }
    
            System.out.println("==========");
            Iterator<Map.Entry<Integer,String>> it = map.entrySet().iterator();
            while(it.hasNext()){
                Map.Entry<Integer, String> entry = it.next();
                System.out.println(entry.getKey() + " , " + entry.getValue());
            }
    
            System.out.println("==========");
            for(String v : map.values()) {
                System.out.println(v);
            }
        }
    
  • 相关阅读:
    Kubernetes部署高可用MongoDB集群
    C博客作业00—我的第一篇博客
    小发现,关于windows窗口中的F10快捷键
    C#小程序——从百度摘取搜索结果。
    elasticsearch 基础语句
    html嵌套html解决办法(<object></object>)
    websocket 前后台代码实例
    css3轮播图实现代码
    validate使用案例
    SiteMap Editor for Microsoft Dynamics CRM 2011
  • 原文地址:https://www.cnblogs.com/loytime/p/11851594.html
Copyright © 2011-2022 走看看