我有一个TreeMap
:
final Map<UserSetting, ItemsIntersectionAndComplement> _usersSimilar = new TreeMap<UserSetting, ItemsIntersectionAndComplement>(new Comparator<UserSetting>() {
@Override
public int compare(UserSetting lhs, UserSetting rhs) {
int prec = (lhs.id()).compareTo(rhs.id());
return lhs.totalMatch > rhs.totalMatch ? -1 : (lhs.totalMatch < rhs.totalMatch ) ? 1 : prec;
}
});
当检查 key 是否存在时,它响应 false,但在特定的第 n 个位置我看到该项目。这怎么可能?并且在之前的迭代中添加了项目。
if (!_usersSimilar.containsKey(rating2.userSetting())) { // <-- CHECKING HERE EXISTENCE
ItemsIntersectionAndComplement iiac = new ItemsIntersectionAndComplement();
iiac.intersection.add(rating2.item());
rating2.userSetting().totalMatch = 1;
_usersSimilar.put(rating2.userSetting(), iiac);
} else {
//.. SOME CODE, BUT CALL DOES NOT REACH IT
}
public class UserSetting extends _UserSetting {
public UserSetting() {
}
int totalMatch;
}
最佳答案
if (!_usersSimilar.containsKey(rating2.userSetting())) { // <-- CHECKING HERE EXISTENCE
ItemsIntersectionAndComplement iiac = new ItemsIntersectionAndComplement();
iiac.intersection.add(rating2.item());
rating2.userSetting().totalMatch = 1;
_usersSimilar.put(rating2.userSetting(), iiac);
}
您在 Map 中搜索的键和您放入 Map 中的键具有不同的状态,因为您正在 if 子句中更新 rating2.userSetting().totalMatch
。
因此,您正在搜索的键很可能在 map 中不存在,但您要添加的键在 map 中存在。
关于Java TreeMap 出现故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41259999/