Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to write a method that will return an ArrayList of Strings from my HashMap.

Currently I have a HashMap that contains a String as the identifier (key?), and a List of type String that holds all the values associated with it. The program is meant to mimic a train/subway navigation tool, so the first String is the station name, and the arraylist is a series of Strings showing all the connected stations.

Here is what I have so far, but it won't currently compile. There are several other methods which I know are working, so I've just put the final one which I'm having difficulty with (getConnections).

I'm very much a novice at this, so if someone could point out where I've gone wrong I'd really appreciate it.

public class MyNetwork implements Network {
    Map<String, List<String>> stations = new HashMap<>();

    @Override
    public String[] getConnections(String fromStation) {

    /**
     * Get a list of all stations directly connected to a given station.
     *
     * @pre fromStation has been added to the network by the method
     * addStation.
     * @param fromStation
     * @return A list of all the stations to which there is a direct
     * connection from fromStation.
     */
    ArrayList<String> Connections = new ArrayList<>();
    Set<String> keys = stations.keySet();

    for (String k : keys) {
        String keyValue;
        keyValue = (stations.get(fromStation));
    }

    return fromStation;
}
share|improve this question

1 Answer 1

up vote 2 down vote accepted

There's no need to explicitly iterate over the values in the Map, just use built-in methods. The whole getConnections() implementation can be written in a single line:

return stations.get(fromStation).toArray(new String[0]);

How it works:

  • First we obtain the list of connections for a given station, using get()
  • The previous step returns a List<String>
  • Now we only need to convert it to a String[] using toArray(). For type safety we pass an array of the expected return type

Alternatively, you could also change the return type to List<String>, unless strictly necessary there's no need to convert a List to an array; if you decide to do this the toArray() call would be unnecessary.

share|improve this answer
    
Thanks for the explanation Oscar, really helped! I've had some suggestions that perhaps I would have been better off using a MultiMap as the basis for my code, as opposed to a hashmap. Would your solution be appropriate for that as well, or would it be very dependant on my re-implementation? –  JavaStarta Feb 15 at 16:05
    
My pleasure! What you have implemented is indeed a multimap. If it's working I'd leave it alone, but there are existing implementations, for instance the interfaces and concrete classes defined in Apache Commons or in Google Guava. My solution might or might not need to be adapted for using those it's implementation-dependent. Check them out and see if they fit ;) –  Óscar López Feb 15 at 17:10
    
So in the same context as that, this line of code (which should return an array containing all the stations now added to the network as strings), doesn't seem to work? The compiler doesn't seem to like passing this information to a new array? Do I have to convert this to a string before I can add the values to the new array? public String[] getStations() { return stations.toArray(new String[0]); –  JavaStarta Feb 16 at 12:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.