zoukankan      html  css  js  c++  java
  • 数组去重复及记录重复个数(以及遍历map的四种方法)

    private static void check(String[] array) {
            // 字符串数组中,含有不重复的字符串有哪些?每一个重复的个数        
            Map<String,Integer> map = new HashMap<>();        
            for(int i=0;i<array.length;i++){
                if(map.get(array[i]) != null){   
                    map.put(array[i], map.get(array[i]) + 1);// value + 1
                } else {
                    map.put(array[i],1);
                }
            }
    
            // ①遍历map
            System.out.println("通过map.keySet()遍历key和value:");
            for(String key:map.keySet()){
                System.out.println("The array is:" + key + "= " + map.get(key));
            }        
            // ②mapSet().iterator()
            System.out.println("通过map.entrySet()的iterator()遍历key和value");
            Iterator<Entry<String, Integer>> it = map.entrySet().iterator();
            while(it.hasNext()){
                Entry<String, Integer> entry = it.next();
                System.out.println("The array is:" + entry.getKey() + "= " + entry.getValue());
            }
            // ③ mapSet() 容量大时推荐
            System.out.println("通过Map.entrySet()遍历key和value");        
            for(Entry<String, Integer> entry:map.entrySet()){
                System.out.println("The array is:" + entry.getKey() + "= " + entry.getValue());
            }        
            //
            System.out.println("通过Map.values()遍历所有的value,但不能遍历key");        
            for(Integer value: map.values()){
                System.out.println("= " + value); 
            }      
        }
  • 相关阅读:
    解决IE8不兼容通过class名获取元素的方法
    移动端页面遇到过的各种坑
    强大的正则表达式
    弹性盒子布局
    vue环境搭建
    fullpage.js使用指南
    ES5原生api(1)
    双色球中奖率分析(python)
    使用python脚本的3D引擎Panda3d
    Python lambda介绍
  • 原文地址:https://www.cnblogs.com/ysloong/p/6431164.html
Copyright © 2011-2022 走看看