尝试对 HashMap 进行排序但未产生正确的结果
Trying To Sort HashMap But Not Produce Correct Result
我有一个 HashMap,它有 未排序的值
[{cols=2, row=2}, {cols=0, row=2}, {cols=6, row=1}, {cols=8, row=1}, {cols=4, row=1}
, {cols=10, row=1}, {cols=2, row=1}, {cols=0, row=1}, {cols=8, row=4}, {cols=6, row=2},
{cols=8, row=2}, {cols=4, row=2}, {cols=6, row=4}, {cols=2, row=4}, {cols=10, row=2},
{cols=4, row=4}, {cols=10, row=4}, {cols=0, row=4}]
当我尝试使用 cols 进行排序时,我得到了这样的结果 排序后的值
[{cols=0, row=2}, {cols=0, row=1}, {cols=0, row=4}, {cols=10, row=1}, {cols=10, row=2},
{cols=10, row=4}, {cols=2, row=2}, {cols=2, row=1}, {cols=2, row=4}, {cols=4, row=1},
{cols=4, row=2}, {cols=4, row=4}, {cols=6, row=1}, {cols=6, row=2}, {cols=6, row=4},
{cols=8, row=1}, {cols=8, row=4}, {cols=8, row=2}]
它应该产生 0,2,4,6,8,10 而不是这些它产生 0,10,2,4,6,8
我对 hashmap 进行排序的代码是
Collections.sort(upperlist, mapComparatorCols)
public Comparator<Map<String, String>> mapComparatorCols = new Comparator<Map<String, String>>() {
public int compare(Map<String, String> m1, Map<String, String> m2) {
return m1.get("cols").compareTo(m2.get("cols"));
}
};
HashMap本身不能保证顺序:
发件人:http://developer.android.com/reference/java/util/HashMap.html
Note that the iteration order for HashMap is non-deterministic. If you want deterministic iteration, use LinkedHashMap.
或者更一般地说,在 Java 文档中:
http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html
This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.
如果您想按顺序访问它们,请使用 TreeMap:
根据评论对代码进行了更新
众所周知,HashMap 默认不保留任何顺序。如果有需要,您需要根据需求使用 TreeMap 或 Comparators 对其进行显式排序。
在您的例子中,听起来您是根据基于字符串的标准进行排序。有了它,10 在 2 之前,等等
您是否映射了一组字符串或是否在比较器中将它们转换为字符串?不清楚。无论如何,对于这两个假设,这是一个快速而肮脏的解决方案。
如果它们真的是字符串:
public Comparator<Map<String, String>> mapComparatorCols =
new Comparator<Map<String, String>>() {
public int compare(Map<String, String> m1, Map<String, String> m2) {
int foo1 = Integer.parseInt(m1.get("cols"));
int foo2 = Integer.parseInt(m2.get("cols"));
// two alternative returns
// faster giving the distance
return (foo1 - foo2);
// safer with no distance
return (foo1 < foo2) ? -1 : (foo1 > foo2) ? +1 : 0;
}
};
如果它们真的是整数:
public Comparator<Map<int, int>> mapComparatorCols =
new Comparator<Map<int, int>>() {
public int compare(Map<int, int> m1, Map<int, int> m2) {
// change it using ?: operator, if you prefer (see above)
return (m1.get("cols") - m2.get("cols"));
}
};
我尝试这个时遇到了同样的问题。经过一些搜索,我尝试了下面的代码,它运行良好。
class MapComparetor implements Comparator<Map<String, String>> {
private String key = "";
public MapComparetor(String key) {
this.key = key;
}
@Override
public int compare(Map<String, String> lhs, Map<String, String> rhs) {
String firstvalue = lhs.get(key);
String secondvalue = rhs.get(key);
if (Integer.valueOf(firstvalue) >= Integer.valueOf(secondvalue)) {
return 1;
} else {
return -1;
}
}
}
做空效果很好
我有一个 HashMap,它有 未排序的值
[{cols=2, row=2}, {cols=0, row=2}, {cols=6, row=1}, {cols=8, row=1}, {cols=4, row=1}
, {cols=10, row=1}, {cols=2, row=1}, {cols=0, row=1}, {cols=8, row=4}, {cols=6, row=2},
{cols=8, row=2}, {cols=4, row=2}, {cols=6, row=4}, {cols=2, row=4}, {cols=10, row=2},
{cols=4, row=4}, {cols=10, row=4}, {cols=0, row=4}]
当我尝试使用 cols 进行排序时,我得到了这样的结果 排序后的值
[{cols=0, row=2}, {cols=0, row=1}, {cols=0, row=4}, {cols=10, row=1}, {cols=10, row=2},
{cols=10, row=4}, {cols=2, row=2}, {cols=2, row=1}, {cols=2, row=4}, {cols=4, row=1},
{cols=4, row=2}, {cols=4, row=4}, {cols=6, row=1}, {cols=6, row=2}, {cols=6, row=4},
{cols=8, row=1}, {cols=8, row=4}, {cols=8, row=2}]
它应该产生 0,2,4,6,8,10 而不是这些它产生 0,10,2,4,6,8
我对 hashmap 进行排序的代码是
Collections.sort(upperlist, mapComparatorCols)
public Comparator<Map<String, String>> mapComparatorCols = new Comparator<Map<String, String>>() {
public int compare(Map<String, String> m1, Map<String, String> m2) {
return m1.get("cols").compareTo(m2.get("cols"));
}
};
HashMap本身不能保证顺序:
发件人:http://developer.android.com/reference/java/util/HashMap.html
Note that the iteration order for HashMap is non-deterministic. If you want deterministic iteration, use LinkedHashMap.
或者更一般地说,在 Java 文档中: http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html
This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.
如果您想按顺序访问它们,请使用 TreeMap:
根据评论对代码进行了更新
众所周知,HashMap 默认不保留任何顺序。如果有需要,您需要根据需求使用 TreeMap 或 Comparators 对其进行显式排序。
在您的例子中,听起来您是根据基于字符串的标准进行排序。有了它,10 在 2 之前,等等
您是否映射了一组字符串或是否在比较器中将它们转换为字符串?不清楚。无论如何,对于这两个假设,这是一个快速而肮脏的解决方案。
如果它们真的是字符串:
public Comparator<Map<String, String>> mapComparatorCols =
new Comparator<Map<String, String>>() {
public int compare(Map<String, String> m1, Map<String, String> m2) {
int foo1 = Integer.parseInt(m1.get("cols"));
int foo2 = Integer.parseInt(m2.get("cols"));
// two alternative returns
// faster giving the distance
return (foo1 - foo2);
// safer with no distance
return (foo1 < foo2) ? -1 : (foo1 > foo2) ? +1 : 0;
}
};
如果它们真的是整数:
public Comparator<Map<int, int>> mapComparatorCols =
new Comparator<Map<int, int>>() {
public int compare(Map<int, int> m1, Map<int, int> m2) {
// change it using ?: operator, if you prefer (see above)
return (m1.get("cols") - m2.get("cols"));
}
};
我尝试这个时遇到了同样的问题。经过一些搜索,我尝试了下面的代码,它运行良好。
class MapComparetor implements Comparator<Map<String, String>> {
private String key = "";
public MapComparetor(String key) {
this.key = key;
}
@Override
public int compare(Map<String, String> lhs, Map<String, String> rhs) {
String firstvalue = lhs.get(key);
String secondvalue = rhs.get(key);
if (Integer.valueOf(firstvalue) >= Integer.valueOf(secondvalue)) {
return 1;
} else {
return -1;
}
}
}
做空效果很好