統計List中的元素重複出現的次數 【轉】
阿新 • • 發佈:2018-10-31
原連結地址 https://blog.csdn.net/chengyabingfeiqi/article/details/49906875
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: 1 b: 2 c: 2 a: 4 例子 3 - 用Map統計每個元素出現的頻率 Key : d Value : 1 Key : b Value : 2 Key : c Value : 2 Key : a Value : 4 Sorted Map Key : a Value : 4 Key : b Value : 2 Key : c Value : 2 Key : d Value : 1