我尝试对哈希图进行排序并返回结果键列表。这是我试过的:
public List<City> getCities() {
Map<Node<City>, Edge> map = new HashMap<>();
// add entries to map ...
return map.entrySet().stream()
.sorted(Map.Entry.<Node<City>, Edge>comparingByValue())
.map(e -> e.getKey().getValue())
.collect(Collectors.toList());
}
边缘实现可兼容,getValue() 方法将返回一个 City 对象。
public class Edge<T> implements Comparable<Edge<T>> {
private Node<T> a;
private Node<T> b;
private double weight;
public Edge(Node<T> a, Node<T> b) {
this.a = a;
this.b = b;
}
public void setWeight(double weight) {
this.weight = weight;
}
public double getWeight() {
return sum;
}
@Override
public int compareTo(Edge<T> o) {
return Double.compare(getWeight(), o.getWeight());
}
}
现在我的 IDE 没有抱怨任何东西,但是当我编译时我得到这个错误:
错误:(104, 28) java: 找不到符号符号:方法 getKey() 位置:java.lang.Object 类型的变量 e
我知道只发布一些代码和一个错误是不好的。但实际上我搜索了一个小时,一无所获。同样对我来说,代码绝对有意义,我不知道为什么无法识别条目的 getKey。
那么发生了什么?为什么我会收到此错误?
ITMISS
摇曳的蔷薇
幕布斯6054654
随时随地看视频慕课网APP
相关分类