DESKTOP-IL3AS1R_20200910-陈鑫

问题:

键集

public static void main(String[] args) {
        Map<String,Integer> map = Map.of( "杨树" , 25 , "柳树" , 40 , "银杏" , 15, "松树" , 10 );
        System.out.println( map );
        // 获取所有的key组成的Set集合
        Set<String> keys = map.keySet();
    
        for( String key : keys ) {
            Integer value  = map.get(key);
            System.out.println(key + ":" + value);
        }
        
        System.out.println( "- - - - - - - - -" );
        
        Iterator<String> itor = keys.iterator();
        while(itor.hasNext()) {
            String key = itor.next();
            Integer value = map.get(key);
            System.out.println(key + ":" + value);
        }
    }

值集

public static void main(String[] args) {
        Map<String,Integer> map = Map.of( "杨树" , 25 , "柳树" , 40 , "银杏" , 15, "松树" , 10 );
        System.out.println( map );
        // 获取所有的value组成的Collection集合
        Collection<Integer> values = map.values();
    
        for( Integer value : values ) {
            System.out.println(value);
        }
        
        System.out.println( "- - - - - - - - -" );
        
        Iterator<Integer> itor = values.iterator();
        while(itor.hasNext()) {
            Integer value = itor.next();
            System.out.println(value);
        }
    }

键值集

public static void main(String[] args) {
        Map<String,Integer> map = Map.of( "杨树" , 25 , "柳树" , 40 , "银杏" , 15, "松树" , 10 );
        System.out.println( map );
        // 获取所有的Entry组成的Set集合
        Set< Map.Entry<String,Integer> > entrys = map.entrySet();
        
        for( Map.Entry<String,Integer> entry : map.entrySet()) {
            String key = entry.getKey();
            Integer value = entry.getValue();
            System.out.println( key + ":" +value );
        }
        
        System.out.println( "- - - - - - - - -" );
        
        Iterator< Map.Entry<String,Integer> > itor = entrys.iterator();
        while(itor.hasNext()) {
            Map.Entry<String,Integer> entry = itor.next();
            String key = entry.getKey();
            Integer value = entry.getValue();
            System.out.println( key + ":" +value );
        }
    }

吐槽:

祝各位老师教师节快乐!

标签

© 2021 成都云创动力科技有限公司 蜀ICP备20006351号-1