import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.HashSet;import java.util.List;import java.util.Map;import java.util.Set;import java.util.TreeMap;public class CountDuplicatedList {public static void main(String[] args) {List list = new ArrayList(); list.add("a"); list.add("b"); list.add("c"); list.add("d"); list.add("b"); list.add("c"); list.add("a"); list.add("a"); list.add("a"); System.out.println("\n例子 1 -统计'a'出现的频率"); System.out.println("a : " + Collections.frequency(list, "a")); System.out.println("\n例子 2 - 统计每一个元素出现的频率"); //将List转换为Set Set uniqueSet = new HashSet(list); for (String temp : uniqueSet) { System.out.println(temp + ": " + Collections.frequency(list, temp)); } System.out.println("\n例子 3 - 用Map统计每个元素出现的频率"); Map map = new HashMap(); for (String temp : list) { Integer count = map.get(temp); map.put(temp, (count == null) ? 1 : count + 1); } printMap(map); System.out.println("\nSorted Map"); Map treeMap = new TreeMap(map); printMap(treeMap); } public static void printMap(Map map){ for (Map.Entry entry : map.entrySet()) { System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue()); } } }
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("b");
list.add("c");
list.add("a");
list.add("a");
list.add("a");
System.out.println("\n例子 1 -统计'a'出现的频率");
System.out.println("a : " + Collections.frequency(list, "a"));
System.out.println("\n例子 2 - 统计每一个元素出现的频率");
//将List转换为Set
Set uniqueSet = new HashSet(list); for (String temp : uniqueSet) { System.out.println(temp + ": " + Collections.frequency(list, temp)); } System.out.println("\n例子 3 - 用Map统计每个元素出现的频率"); Map map = new HashMap(); for (String temp : list) { Integer count = map.get(temp); map.put(temp, (count == null) ? 1 : count + 1); } printMap(map); System.out.println("\nSorted Map"); Map treeMap = new TreeMap(map); printMap(treeMap); } public static void printMap(Map map){ for (Map.Entry entry : map.entrySet()) { System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue()); } } }
for (String temp : uniqueSet) {
System.out.println(temp + ": " + Collections.frequency(list, temp));
}
System.out.println("\n例子 3 - 用Map统计每个元素出现的频率");
Map map = new HashMap(); for (String temp : list) { Integer count = map.get(temp); map.put(temp, (count == null) ? 1 : count + 1); } printMap(map); System.out.println("\nSorted Map"); Map treeMap = new TreeMap(map); printMap(treeMap); } public static void printMap(Map map){ for (Map.Entry entry : map.entrySet()) { System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue()); } } }
for (String temp : list) {
Integer count = map.get(temp);
map.put(temp, (count == null) ? 1 : count + 1);
}
printMap(map);
System.out.println("\nSorted Map");
Map treeMap = new TreeMap(map); printMap(treeMap); } public static void printMap(Map map){ for (Map.Entry entry : map.entrySet()) { System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue()); } } }
printMap(treeMap);
}
public static void printMap(Map map){ for (Map.Entry entry : map.entrySet()) { System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue()); } } }
for (Map.Entry entry : map.entrySet()) { System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue()); } } }
System.out.println("Key : " + entry.getKey() + " Value : "
+ entry.getValue());
}
}
}
结果:
例子 1 -统计'a'出现的频率a : 4例子 2 - 统计每一个元素出现的频率d: 1b: 2c: 2a: 4例子 3 - 用Map统计每个元素出现的频率Key : d Value : 1Key : b Value : 2Key : c Value : 2Key : a Value : 4Sorted MapKey : a Value : 4Key : b Value : 2Key : c Value : 2Key : d Value : 1
统计List中的元素重复出现的次数
最新推荐文章于 2024-07-13 03:02:52 发布