java - 归还藏品

标签 java collections

我正在尝试返回一个集合,其中包含在特定站点开始或结束的网络中的连接。我无法弄清楚如何返回它并获取站参数。在方法中创建 hashMap 是否也是正确的方法,还是应该在它之外创建?

它给我错误 incompatible types: Connection cannot be converted to Collection<Connection>对于返回语句

代码:

/**
 * Return a Collection containing all the Connections in the network that
 * start or end at a specified station
 *
 * @param station Station to/from which the Connection should run
 *
 * @return a Collection containing all the connections that start or end at
 * the specified station
 */

@Override
public Collection<Connection> getConnectionsFrom(Station station) {
    Map<Station, Connection> stationConnectionFrom = new HashMap<Station, Connection>();
    return stationConnectionFrom.get(station);
}

最佳答案

只返回一个Connection。您可以将返回类型更改为:

public Connection getConnectionFrom(Station station) {
    Map<Station, Connection> stationConnectionFrom = new HashMap<>();
    return stationConnectionFrom.get(station);
}

在您的情况下, map 为空,这将始终返回 null

关于java - 归还藏品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36607594/

相关文章:

java - 包装 JUnit 测试(在 Eclipse 中)

java - 在 Crypto++ 中使用 AES 和 CBC 的 ECIES

java - 在实现具有两个职责的接口(interface)的类上实现单一职责原则

JAVA显示图像-GUI

java - 使用 Java8 Stream 将复杂数据映射到对象

java - 泛型集合

c# - 我可以有条件地使用 LINQ 创建 IEnumerable 吗?

java - 为什么 LinkedHashSet 的 boolean accessOrder 设置为 false

java - 为什么我收到错误 'Operation not allowed after ResultSet closed' ?

java - 使用比较器在 ArrayList 中搜索