0

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);
 }
 }
asked Nov 16, 2020 at 0:39
2
  • 1
    Welcome to SO. It's unclear to me what your data structure is from the question. For example 'linked list with multiple values in each cell' isn't actually possible in the LinkedList class. Similarly 'hashmap with the destination of the flight along with the departure time and arrival time all in one cell of the linked list' and 'each cell inside of a hash' are hard to picture without code. Could you provide some code to make it clearer how your structure works? Commented Nov 16, 2020 at 0:43
  • Sorry about that, I have put the code that adds all the information into the hashmap/linked list. To be honest, this is my first time using a hashmap so I am doing this purely based off the videos on youtube and other questions that were asked here by others so my knowledge is very limited. Would it help if i posted the section of the instructions where he explains what he wants? Commented Nov 16, 2020 at 1:04

1 Answer 1

1

Just a few general pointers first:

  • When you use the word 'hash' in your question you really mean 'map'. Using a hash is a common way to implement a map but it is not the only way (a search tree is another common technique). Best to use the name of the structure rather than the implementation
  • Similarly, you should declare your variables using the appropriate interface rather than the implementation class. See below for examples
  • records have been introduced to Java. If you are using a recent version of Java they are a good way to define straight data records such as your Info
  • there are methods in Map to make it easy to handle missing keys without using if statements

So 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.

answered Nov 16, 2020 at 3:01
4
  • Thank you for all the advice, I unfortunately have been told by my professor that ArrayLists are not allowed (not sure why) so I am stuck using LinkedLists as that is what he has suggested we use. For the code extracting the departure and arrival time of each flight leaving an airport my IDE is showing an error for departureTime and arrivalTime as they are not methods. Is this just something I'm misunderstanding and an error on my end or could the double colon not be the correct operator? Thanks so much for your help. Commented Nov 16, 2020 at 4:15
  • The reason I am using this method of using the departure and arrival times is because my professor wants us using a binary heap in the form of an array filled with the flights leaving the airport along with an extract min method to find the shortest path to another given airport based on time. Not sure if there is an easier way to do it that I'm not understanding but that's just how it worked in my head. Commented Nov 16, 2020 at 4:26
  • The arrivalTime and departureTime methods are generated by the record in my code. If you don't use a record they won't exist. Using a linked list instead of array list is fine - they implement the same List interface. Your comments about implementing a binary heap in the form of any array has left me confused about what your actual question is. Commented Nov 16, 2020 at 5:22
  • Gotcha, I am hesitant to use the record implementation because they way we submit is through an auto grader on gradescope and therefore it not being up to date with java 14 could cause my program to fail the test cases. Is there a way to do it without using the record? Sorry my second comment was not clear. I believe this project is to make us familiar with a temporal graph and Dijkstra’s algorithm and therefore uses the array and map to accomplish this as stated in the first image I attached to the original post. Thanks for your help. Commented Nov 16, 2020 at 5:35

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.