【发布时间】:2018-04-11 15:03:15
【问题描述】:
我有一个树状图:
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
.
.
.
为什么会这样?以及如何在第二个循环中获得排序结果?排序是指与我在第一个循环中添加它们的顺序相同吗?谢谢
【问题讨论】: