我有一个HashMap定义如下
Map<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();然后,我将数据存储在这个hashmap中的数据库中,并在控制台上显示如下内容,其中->是ID的左侧条目,右边的条目是该ID使用的标记
165767--->[dual-boot, windows, uninstall, ati, graphics, multiple-monitors]
6873 --->[kubuntu, re-installation]
34228--->[11.10, unity, launcher, libreoffice, icons]我希望根据它们使用的标签数量(即基于map.get(key).size() )对ID进行降序排序,这样输出应该是ID 165767,然后是34228,然后是6873等等。
我试着用TreeMap来做这件事,但是我无法根据大小而不是键的值以及按降序计算出如何实现它。
发布于 2013-11-09 20:42:56
编辑:
我的输出应该根据标签的数量而不是ID的大小进行排序。
Map<String, ArrayList<String>> map = new TreeMap<String, ArrayList<String>>();
map.put("165767",new ArrayList<String>(Arrays.asList("dual-boot", "dual-boot", "windows", "uninstall", "ati", "graphics", "multiple-monitors")));
map.put("6873",new ArrayList<String>(Arrays.asList("kubuntu", "kubuntu", "re-installation")));
map.put("0000000000000000",new ArrayList<String>(Arrays.asList("test","test", "test")));
map.put("0125",new ArrayList<String>(Arrays.asList("dual-boot", "windows", "uninstall", "ati", "graphics", "multiple-monitors")));
for(ArrayList<String> l : map.values()){
Set<String> hs = new HashSet<>();
hs.addAll(l);
l.clear();
l.addAll(hs);
}
List<ArrayList<String>> l = new ArrayList<>(map.values());
Collections.sort(l, new Comparator<ArrayList<String>>(){
public int compare(ArrayList<String> s1, ArrayList<String> s2){
return Integer.compare(s2.size(), s1.size());
}});
for(ArrayList<String> a : l){
Iterator<Entry<String, ArrayList<String>>> iter = map.entrySet().iterator();
while (iter.hasNext()) {
Entry<String, ArrayList<String>> e = iter.next();
if(e.getValue().equals(a)){
System.out.println(e.getKey() + "-" + a);
iter.remove();
}
}
}产出:
0125-[uninstall, dual-boot, graphics, windows, ati, multiple-monitors]
165767-[uninstall, dual-boot, graphics, windows, ati, multiple-monitors]
6873-[re-installation, kubuntu]
0000000000000000-[test]发布于 2013-11-09 20:36:00
这将创建一个已排序的ids列表。
List<String> sortedIds = new ArrayList<String>(map.getKeys());
Collections.sort(sortedIds, new Comparator<String>() {
public int compare(String a, String b) {
return map.get(b).size() - map.get(a).size();
}
});并不是说您永远不会维护一个SortedMap (比如TreeMap),它是根据可变的值排序的(比如ArrayList的长度)。由于排序顺序用于查找值,因此如果"id123"比"id456"大,而集合不知道它,则可能会导致非常大的问题。
发布于 2015-05-25 01:23:21
我有一个类似的场景,这段代码对我来说很有用(有时我有空):
private static Map<Object,List<Object>> sortByArraySizeDesc(Map<Object,List<Object>> map) {
List<List<Object>> list = new LinkedList(map.entrySet());
Collections.sort(list, new Comparator() {
public int compare(Object o1, Object o2) {
if (o1 == null && o2 == null) { return 0; }
else if (o1 == null) { return 1;}
else if (o2 == null) { return -1; }
int size1 = ((List) ((Map.Entry) (o1)).getValue()).size();
int size2 = ((List) ((Map.Entry) (o2)).getValue()).size();
return size2 - size1;
}
});
Map res = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry)it.next();
res.put(entry.getKey(), entry.getValue());
}
return res;
} https://stackoverflow.com/questions/19882717
复制相似问题