gpt4 book ai didi

java - Java中Treemap的headmap方法

转载 作者:行者123 更新时间:2023-11-30 02:05:54 27 4
gpt4 key购买 nike

我正在检查 TreeMap 的 headMap 方法,该方法返回 Map 的一部分,其键严格小于 toKey。所以我期望输出是 B, C 但它只返回 B。这是我做的一件奇怪的事情,我改变了 CompareTo 方法,就像这样 return this.priority > o.priority ? 1 : -1; 然后它开始返回 C, B,这是我所期待的。我确信这是不正确的,但是我怎样才能同时获得优先级低于 A 的 B、C。我哪里出错了。谢谢。

NavigableMap<PolicyTypePriorityWrapper, String> treeMap = new TreeMap();
PolicyTypePriorityWrapper a = new PolicyTypePriorityWrapper("A", 2);
PolicyTypePriorityWrapper b = new PolicyTypePriorityWrapper("B", 1);
PolicyTypePriorityWrapper c = new PolicyTypePriorityWrapper("C", 1);

treeMap.put(a, "A");
treeMap.put(b, "B");
treeMap.put(c, "C");

NavigableMap<PolicyTypePriorityWrapper, String> map = treeMap.headMap(a, false);
Set<PolicyTypePriorityWrapper> policyTypePriorityWrappers = map.keySet();

for (PolicyTypePriorityWrapper pol: policyTypePriorityWrappers) {
System.out.println(pol.getPolicyType());
}

PolicyTypePriorityWrapper.java

class PolicyTypePriorityWrapper implements Comparable<PolicyTypePriorityWrapper> {

private String policyType;
private int priority;

public PolicyTypePriorityWrapper(final String policyType, final int priority) {
this.policyType = policyType;
this.priority = priority;
}

public String getPolicyType() {
return this.policyType;
}

public int getPriority() {
return this.priority;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

PolicyTypePriorityWrapper that = (PolicyTypePriorityWrapper) o;

if (priority != that.priority) return false;
return policyType.equals(that.policyType);
}

@Override
public int hashCode() {
int result = policyType.hashCode();
result = 31 * result + priority;
return result;
}

@Override
public int compareTo(final PolicyTypePriorityWrapper o) {
return Integer.compare(this.priority, o.priority);
}
}

最佳答案

那是因为您没有遵循 JDK 文档指南,来自 Comprarable :

It is strongly recommended (though not required) that natural orderings be consistent with equals. This is so because sorted sets (and sorted maps) without explicit comparators behave "strangely" when they are used with elements (or keys) whose natural ordering is inconsistent with equals. In particular, such a sorted set (or sorted map) violates the general contract for set (or map), which is defined in terms of the equals method.

正如您所看到的,您可能会遇到 a.compareTo(b) == 0!a.equals(b) 的情况。对于 TreeMap"B", 1"C", 1 被认为是相等的:

Note that the ordering maintained by a tree map, like any sorted map, and whether or not an explicit comparator is provided, must be consistent with equals if this sorted map is to correctly implement the Map interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Map interface is defined in terms of the equals operation, but a sorted map performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the sorted map, equal. The behavior of a sorted map is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Map interface.

For example, if one adds two keys a and b such that (!a.equals(b) && a.compareTo(b) == 0) to a sorted set that does not use an explicit comparator, the second add operation returns false (and the size of the sorted set does not increase) because a and b are equivalent from the sorted set's perspective.

所以发生的情况是,您 compareTo 无法区分具有相同优先级但不同类型的两个元素,但由于 TreeMap ONLY 使用该方法来决定是否两个元素相等,那么您首先就不会将它们都添加到 map 中。

你是否尝试过treeMap.size() == 3?我的猜测是,首先是 2。

关于java - Java中Treemap的headmap方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51387694/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com