Reputation: 1
Assignment instructions My professor wants us to use a hashmap along with an array to find the fastest route from one airport to another by plane given a list of flights. He wants us to populate the array as we traverse the hashmap finding the fastest flights from each airport to take. I have gotten as far as creating the hashmap with the destination of the flight along with the departure time and arrival time all in one cell of the linked list but when I attempt to extract the departure and arrival time of each flight leaving the airport to insert them into the array I cannot. Is there a way to extract a specific value from a linked list with multiple values in each cell inside of a hash or should I go about it another way? I have confirmed that the hashmap has the correct information and is functioning correctly I am just unable to or do not know how to access the information inside of the hashmap to send it through to the array. What the information inside the debugger looks like
LinkedHashMap<String, LinkedList<Info>> airportsHash = new LinkedHashMap<String, LinkedList<Info>>();
LinkedList<Info> destinations;
public Info (String destination, double departureTime, double arrivalTime){
this.destination = destination;
this.departureTime = departureTime;
this.arrivalTime = arrivalTime;
}
public void fillHash(String origin, String destination, double departureTime, double arrivalTime) {
if(airportsHash.containsKey(origin)){
destinations = airportsHash.get(origin);
destinations.add(new Info(destination, departureTime/100, arrivalTime/100));
}
else{
destinations = new LinkedList<Info>();
destinations.add(new Info(destination, departureTime/100, arrivalTime/100));
airportsHash.put(origin, destinations);
}
}
Upvotes: 0
Views: 109
Reputation: 27976
Just a few general pointers first:
Info
Map
to make it easy to handle missing keys without using if
statementsSo making those changes:
record FlightInfo(String destination, double departureTime, double arrivalTime) { }
Map<String,List<FlightInfo>> airportFlightMap = new HashMap<>();
public void addFlight(String origin, String destination, double departureTime, double arrivalTime) {
airportFlightMap
.computeIfAbsent(origin, o -> new ArrayList<>())
.add(new FlightInfo(destination, departureTime, arrivalTime));
}
Even better would be to have an Airport
class that contains the flights but that's beyond the scope of your question.
when I attempt to extract the departure and arrival time of each flight leaving the airport to insert them into the array I cannot. Is there a way to extract a specific value from a linked list with multiple values in each cell inside of a hash or should I go about it another way?
Taking your question literally, the way to extract the departure and arrival time of each flight leaving an airport you would do:
List<Double> departureTimes = airportFlightMap.get(airportName)
.stream().map(FlightInfo::departureTime).collect(Collectors.toList());
List<Double> arrivalTimes = airportFlightMap.get(airportName)
.stream().map(FlightInfo::arrivalTime).collect(Collectors.toList());
Though I'm not sure how that gets you closer to your goal of finding the fastest path from one airport to another. For that you would need a search algorithm. For example, in pseudo code
find path(current route, destination):
if end of current route = destination:
process path
else
for each flight from end of current route:
if next airport not in current route:
find path(current route + next airport, destination)
In that algorithm you probably want to store the origin and a list of FlightInfo
as the current route. That way you can easily compare the arrival time to find the best route.
Upvotes: 1