리스트에서 객체 반복 횟수 [회전]를 집계하려면 JDK1이 필요합니다.5+
2052 단어 List 통계 중복
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("
1 - 'a' ");
System.out.println("a : " + Collections.frequency(list, "a"));
System.out.println("
2 - ");
Set uniqueSet = new HashSet(list);
for (String temp : uniqueSet) {
System.out.println(temp + ": " + Collections.frequency(list, temp));
}
System.out.println("
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("
Map - key ");
Map treeMap = new TreeMap(map);
printMap(treeMap);
}
public static void printMap(Map map) {
for (Map.Entry entry : map.entrySet()) {
System.out.println("Key-value : " + entry.getKey() + "- "
+ entry.getValue());
}
}
}