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); 
            }      
        }
  • 相关阅读:
    数据库连接池-配置 wallfilter问题解决-UncategorizedSQLException
    maven统一配置
    maven依赖排除
    list排序
    spring boot日志配置
    HDU 5281 Senior's Gun (贪心)
    Saving HDU (贪心)
    切割木板 (贪心)
    查找最少标记点 (贪心)
    字典序最小问题 (贪心)
  • 原文地址:https://www.cnblogs.com/ysloong/p/6431164.html
Copyright © 2011-2022 走看看