为什么我的树状图没有按 java 排序?
why does my treemap is not sorted in java?
我有一个树状图:
private Map<String, Integer> dataMap = new TreeMap<String, Integer>();
稍后在代码中我有以下部分:
for (String item : data) {
JSONObject itemJson = new JSONObject(item);
System.out.println("-- before " + itemJson.getString("dataLabel") + " " + itemJson.getInt("dataValue"));
dataMap.put(itemJson.getString("dataLabel"), itemJson.getInt("dataValue"));
}
for(String data : dataMap.keySet()) {
System.out.println("-- after " + data);
}
第一个循环显示排序后的元素,如:
-- before January 1
-- before February 2
-- before March 3
-- before April 4
.
.
.
但是第二个循环混合了顺序:
-- after April
-- after February
-- after January
-- after March
.
.
.
为什么会这样?以及如何在第二个循环中获得排序结果?排序的意思是与我在第一个循环中添加它们的顺序相同吗?谢谢
TreeMap
按键而不是按值对条目进行排序。来自文档(强调我的):
A Red-Black tree based NavigableMap implementation. The map is sorted according to the natural ordering of its keys (this is your case), or by a Comparator provided at map creation time, depending on which constructor is used.
输出是正确的,因为字符串是按字母顺序排序的。
how can I get sorted results on the second loop? And by sorted I mean the same order as I was adding them during the first loop?
改用 LinkedHashMap
,因为根据插入的顺序存储条目。来自文档(强调我的):
Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map.
keySet()
方法用于获取此映射中包含的键的 Set 视图。
for (String data : dataMap.keySet())
这给了你钥匙。
现在在该循环中,使用键 (data
) 从 dataMap
获取每个值,并打印它。
检查以下示例
for (Map.Entry<K, V> data: dataMap.entrySet()) {
System.out.println("Key: " + data.getKey() + ". Value: " + data.getValue());
}
或
HashMap
未定义其元素的任何特定顺序。因此,"reverse" 顺序也未定义。
对于TreeMap
,您可以使用descendingMap()
。
我有一个树状图:
private Map<String, Integer> dataMap = new TreeMap<String, Integer>();
稍后在代码中我有以下部分:
for (String item : data) {
JSONObject itemJson = new JSONObject(item);
System.out.println("-- before " + itemJson.getString("dataLabel") + " " + itemJson.getInt("dataValue"));
dataMap.put(itemJson.getString("dataLabel"), itemJson.getInt("dataValue"));
}
for(String data : dataMap.keySet()) {
System.out.println("-- after " + data);
}
第一个循环显示排序后的元素,如:
-- before January 1
-- before February 2
-- before March 3
-- before April 4
.
.
.
但是第二个循环混合了顺序:
-- after April
-- after February
-- after January
-- after March
.
.
.
为什么会这样?以及如何在第二个循环中获得排序结果?排序的意思是与我在第一个循环中添加它们的顺序相同吗?谢谢
TreeMap
按键而不是按值对条目进行排序。来自文档(强调我的):
A Red-Black tree based NavigableMap implementation. The map is sorted according to the natural ordering of its keys (this is your case), or by a Comparator provided at map creation time, depending on which constructor is used.
输出是正确的,因为字符串是按字母顺序排序的。
how can I get sorted results on the second loop? And by sorted I mean the same order as I was adding them during the first loop?
改用 LinkedHashMap
,因为根据插入的顺序存储条目。来自文档(强调我的):
Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map.
keySet()
方法用于获取此映射中包含的键的 Set 视图。
for (String data : dataMap.keySet())
这给了你钥匙。
现在在该循环中,使用键 (data
) 从 dataMap
获取每个值,并打印它。
检查以下示例
for (Map.Entry<K, V> data: dataMap.entrySet()) {
System.out.println("Key: " + data.getKey() + ". Value: " + data.getValue());
}
或
HashMap
未定义其元素的任何特定顺序。因此,"reverse" 顺序也未定义。
对于TreeMap
,您可以使用descendingMap()
。