我正在尝试返回一个集合,其中包含在特定站点开始或结束的网络中的连接。我无法弄清楚如何返回它并获取站参数。在方法中创建 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/