0

I'm having a little difficulty getting my head around a hashmap I'm trying to implement. The basic premise is that I have a list of stations in a txt file, with the "connections" between each. Something like;

Connection: Station1 Station2

Connection: Station4 Station6

I have several methods, which will add a station name as a string, and then store its "connected" stations in an arraylist which I can then later retrieve to show "Station4: Connected to Station 6" and so on.

The two methods below i'm concerned with are the "add stations" and the "add connection" ones I've set it up so the Hashmap "stations" should contain a String > Arraylist relationship. My thinking was that the "String" would store the station name, and then the arraylist "connectedstations" would store all the stations that it's connected to, I just don't know how to create the association? I've written some before using a Key, but I can't seem to get anything to work here!

Any help would be really appreciated! Thanks

public class MyNetwork implements Network {

//The Hashmap of type String, Arraylist. The String holding the 
//station name, and the arraylist holding the stations connected to it

HashMap<String, ArrayList> stations = new HashMap<>();

//the arraylist to hold the connected stations

ArrayList<String> connectedstations = new ArrayList<>();



@Override
public void addStation(String station) {

    //adds a station to the hashmap, pointing to a CURRENTLY empty
    //arraylist "connectedstations"

          stations.put(station,connectedstations);

}

@Override
public void addConnection(String fromStation, String toStation) {

   /**
 * Add a direct connection from one station to another.
 * @pre both fromStation and toStation have already been added by the method
 * addStation.
 * @param fromStation The station from which the connection begins. 
 * @param toStation The station at which the connection ends.
 */


}
JavaStarta
  • 67
  • 1
  • 11
  • 4
    I see so many stubborn people try to make lists of lists instead of creating an object, and they scratch their head for HOURS trying to get the logic right. And then if you need to change one piece, you have to change all of the pieces. Just create a Connection object that contains a list of Station objects, and your life will be so easy. – Rainbolt Feb 14 '14 at 16:32
  • 1
    @John: the logic, in this case, would still be exactly the same: you would still have to get a Connection object (instead of a List) from the map, and add a station to it (instead of adding it to the List). Naming "Connection" a list of stations looks even more confusing to me. What should be in the map as value is a Station object, having a name property, and a list of connected stations. And the logic would still be identical. – JB Nizet Feb 14 '14 at 16:39
  • @JBNizet You misinterpreted my comment. A Connection *is not a* list of Stations. A Connection *has a* list of Stations. I never claimed the logic would change. I claimed that the logic would be easier to understand and easier to maintain. For example, if a Station can't be connected to itself, you could use the `this` reference to check. You can add constraints like that to the *Object*, rather than trying to impose restrictions on a list of lists. I realize that "easier to understand" is subjective, which is why I left my comment as a simply a comment. – Rainbolt Feb 14 '14 at 17:33
  • But a Connection shouldn't have a list of stations. A Connection is a link between a source and a target station. I agree with you, but my point was that naming an object containing a list of stations 'Connection' is even more confusing than simply using a list. As I said, what should be in the map should be a Station object, with a name and a list of connected stations, and indeed, methods allowing to check for constraints. – JB Nizet Feb 14 '14 at 17:37
  • @JBNizet A Connection IS A link. A Connection HAS two endpoints. I understand you think that is too confusing, but it simply isn't. – Rainbolt Feb 14 '14 at 17:43
  • You seem to be much more stubborn than the people you're seeing. First using a List to store a source and a target attributes is not what I would call good design. Second, the OP want to store many connections for a given station. So a Map wouldn't fit, if you accept the definition of Connection as a link between one station and one other station. You've just confused yourself, it seems. – JB Nizet Feb 14 '14 at 17:48

1 Answers1

2

Your current code adds the same list of connections for all the stations added to the map. That can't be right, since every station has its own list of connections.

addStation() should add a station to the map, with a new empty list as value.

addConnection() should get the list of connections associated to the station in the map, and should add a station to this list.

Just a few additional notes:

  • the type of the Map should be Map<String, List<String>>
  • you could use Guava, which has Multimaps, making this process easier: no need to care about initializing an empty list, be able to add directly to the multimap instead of getting the list out of it and adding to the list.
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Sorry, should have mentioned previously. This is a revision exercise, so I'm fairly constricted in the way I can implement it. The method titles and @param bits were what we started with, and that's the way we have to implement it. I'm using NetBeans, I have no idea what Guava is. are you suggesting that in the "addstation" method, where I am currently pointing to an empty arraylist "connected stations", I should be instead creating a new arraylist every time? I just can't seem to get this in my head, and im bashing my skull against the wall to the point of concussion! – JavaStarta Feb 14 '14 at 16:51
  • Yes, that's what I'm suggesting. You need one list, different from all the other ones, for each station. So if you create a single list in your code, that can't be right. FYI: Guava is a popular Java library. Google it and you'll find it. The fact that you're using NetBeans, BTW, is irrelevant. Java code is Java code, whatever the tool you use to create it. – JB Nizet Feb 14 '14 at 16:53
  • Ok, i'm not sure if im now just heading in the wrong direction again, but would this work for the addstations? `public void addStation(String station) {' `List connectedStations = stations.get(station);` `if (connectedStations == null) {` `connectedStations = new ArrayList();` `connectedStations.add(station);` `}` `stations.put(station, connectedStations);` `}` – JavaStarta Feb 14 '14 at 17:26
  • Re-read my answer. You just need to put an empty list in the map: `public void addStation(String station) {stations.put(station, new ArrayList());}`. You don't want the station to be connected with itself. So why would you add the station to the list of connected stations? – JB Nizet Feb 14 '14 at 17:26
  • OK I've got you, I was over thinking as always! So now I have a station stored as a String, and an empty arraylist for each station which will contain its connected station. So keeping to how it is currently, how would I create the association between a Station and its Connected Station? The new Arraylist bit has no name, so how to I refer to it in the code? Is this where I need to keyset or something? Apologies, I know this must be second nature for you but its one step after brutal step for me! – JavaStarta Feb 14 '14 at 17:58
  • It doesn't have a name. But it's stored in the map, under the name of the from station. So you can `get()` the list from the map using the `from` station as the key, and `add()` the `to` station to it. A single line of code is sufficient. – JB Nizet Feb 14 '14 at 18:00
  • I've got this at the moment but it doesn't seem to like the "add" bit, tried it quite a few different ways but it doesn't seem to like it. `stations.get(fromStation,(stations.add(toStation)));` Is it something obvious, or have I missed the logic here? – JavaStarta Feb 14 '14 at 19:12
  • That wouldn't even compile, would it? get() takes the from station as argument, and returns a list. And you want to add the to station to this returned list. – JB Nizet Feb 14 '14 at 20:27
  • A-ha! `stations.get(fromStation).add(toStation);` - Think that's got it? Thanks for your patience JB Nizet, appreciated! – JavaStarta Feb 14 '14 at 21:31
  • You got it. It took you quite a time to find out these two instructions, but hopefully you learnt quite a few things, and much more than if I had given you the solution directly. Good luck for the rest. – JB Nizet Feb 14 '14 at 21:37