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); } }