取集合内元素的个数并排序输出
public static List<String> getDataCount(List<String> list) {
HashMap<String, Integer> listMap = new HashMap<>();
ArrayList<String> strings = new ArrayList<>();
int count = 1;
for (int i = 0; i < list.size() - 1; i++) {
for (int j = list.size() - 1; j > i; j--) {
if (list.get(i).equals(list.get(j))) {
count++;
list.remove(j);
}
}
listMap.put(list.get(i), count);
count = 1;
}
ArrayList<Map.Entry<String, Integer>> entries = new ArrayList<>(listMap.entrySet());
Collections.sort(entries,( o1, o2)->{
return o2.getValue() - o1.getValue();
});
for (Map.Entry<String, Integer> entry : entries) {
strings.add(entry.getKey()+":"+entry.getValue());
}
return strings;
}