我试图返回一个集合,该集合包含在特定站点开始或结束的网络中的连接。我在弄清楚如何返回它并采用station参数时遇到了麻烦。还有在方法中创建hashMap的正确方法,还是应该在该方法之外创建它?
它给我return语句的错误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);
}
在您的情况下,映射为空,它将始终返回
null
。关于java - 退货,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36607594/