我有这段代码:
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);
}
Run Code Online (Sandbox Code Playgroud)
对于具有以下对的地图:2 - UserNotification,3 - UserNotification,4 - UserNotification,5 - UserNotification,6 - UserNotification,7 - UserNotification,8 - UserNotification,9 - UserNotification,10 - UserNotification
代码输出为:10 2 3 4 5 6 7 8 9
考虑到TreeMap按键对所有数据进行排序,这怎么可能呢?我想值10的键应该在列表的末尾.
该TreeMap基于它的键排序按字典(按字母顺序),所以用1开始了任何事情,任何事情之前开始用2等
如果你想以数字方式对地图进行排序,你应该使用aTreeMap<Integer, UserNotification>