1

我有一个树集。

字符串键实际上是一个 int 的字符串值,因为 freemarker 不允许 int 作为映射的键,并且由于这是一个排序的映射,它对它的排序如下:

1
10
11
12
2
3
4
5
6
7
8
9

任何人都知道如何解决这个问题并使其按数字顺序打印?

谢谢

4

2 回答 2

2

Construct the map with an appropriate comparator via the constructor TreeMap(Comparator).

(Or TreeSet, if that's what you actually mean; you mention both sets and maps.)

于 2012-09-03T18:07:18.757 回答
2

String internally imposes a lexicographic order via compareTo.

Compares two strings lexicographically.

If you don't want that, consider specifying a custom Comparator e.g.

final Map<String, ...> map = new TreeMap<String, ...>(new Comparator<String>() {

  public int compare(final String a, final String b) {
    return Integer.valueOf(a).compareTo(Integer.valueOf(b));
  }
});
于 2012-09-03T18:08:29.137 回答