我有这段代码:
private final static TreeMap<String, UserNotification> USER_NOTIFICATION_MAP = new TreeMap<String, UserNotification>();
//Filling the map using services
String idString = "1";
Iterator it = USER_NOTIFICATION_MAP.entrySet().iterator();
while (it.hasNext())
{
Map.Entry pairs = (Map.Entry)it.next();
idString = pairs.getKey().toString();
System.out.println(idString);
}
对于具有以下对的地图:
2 – UserNotification,
3 – UserNotification,
4 – UserNotification,
5 – UserNotification,
6 – UserNotification,
7 – UserNotification,
8 – UserNotification,
9 – UserNotification,
10 – UserNotification
代码的输出是:
10
2
3
4
五
6
7
8
9
考虑到TreeMap按键对所有数据进行排序,这怎么可能呢?我想值10的键应该在列表的末尾.
解决方法:
TreeMap按字典顺序(按字母顺序)对其键进行排序,因此以1开头的任何内容都以2开头的任何内容开头.
如果要以数字方式对地图进行排序,则应使用TreeMap< Integer,UserNotification>