我想不通爲什麼我的自定義UpdateableTreeMap
類不工作。它應該按照它的值對TreeMap
進行排序。TreeMap按價值排序不工作?
全部代碼在這裏:
import org.bukkit.entity.Player;
import java.util.*;
public class UpdateableTreeMap {
private final HashMap<Player, PlayerData> hashMap;
private final TreeMap<Player, PlayerData> treeMap;
public UpdateableTreeMap() {
hashMap = new HashMap<>();
treeMap = new TreeMap<>(new ValueComparator(hashMap));
}
public Map<Player, PlayerData> internalMap() {
return hashMap;
}
public Set<Player> keySet() {
return hashMap.keySet();
}
public boolean containsKey(Object key) {
return hashMap.containsKey(key);
}
public PlayerData get(Object key) {
return hashMap.get(key);
}
public PlayerData remove(Object key) {
treeMap.remove(key);
return hashMap.remove(key);
}
public boolean isEmpty() {
return hashMap.isEmpty();
}
public int size() {
return hashMap.size();
}
public Map.Entry<Player, PlayerData> firstEntry() {
return treeMap.firstEntry();
}
public Set<Map.Entry<Player, PlayerData>> entrySet() {
return hashMap.entrySet();
}
public Set<Map.Entry<Player, PlayerData>> sortedEntrySet() {
return treeMap.entrySet();
}
public Collection<PlayerData> values() {
return hashMap.values();
}
public Collection<PlayerData> sortedValues() {
return treeMap.values();
}
public PlayerData put(Player key, PlayerData value) {
hashMap.put(key, value);
return treeMap.put(key, value);
}
public void update(Player key) {
PlayerData value = treeMap.remove(key);
if (value != null) {
treeMap.put(key, value);
}
}
public static class ValueComparator implements Comparator<Player> {
private final Map<Player, PlayerData> map;
public ValueComparator(Map<Player, PlayerData> map) {
this.map = map;
}
public int compare(Player o1, Player o2) {
if (o1 == o2)
return 0;
PlayerData d1 = map.get(o1);
PlayerData d2 = map.get(o2);
System.out.println(o1.getName() + " " + d1.maxhealth + " - " + d2.maxhealth + " " + o2.getName());
System.out.println("Result: " + (o1 == o2 ? 0 : (d1.maxhealth < d2.maxhealth ? 1 : -1)));
if (d1.maxhealth < d2.maxhealth)
return 1;
return -1;
}
}
}
當我打電話update(Player)
,我可以清楚地看到感謝System.out.println()
線是compare(Player, Player)
將返回-1。但是,當我使用sortedValues()
方法遍歷TreeMap時,順序不正確。
你想要什麼命令?您的代碼首先提供「最健康」。如果它不是您想要的,請更改比較器中的符號。 –
是的,這就是我要找的訂單。但是,它並不一致。 –
嘗試按值對地圖進行排序在大多數情況下從一開始就註定要失敗。比較者破解你正在做的只是爲了解決問題。 http://stackoverflow.com/a/2581754/869736是解決這個問題的唯一可靠方法。 –