Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Generates a HashMap that represents the MTR network with the stations and
- * all their immediate connections.
- *
- * @param lineMap
- * the lines and their respective stations
- * @return a map of the stations and their respective Node object
- */
- public HashMap<String, Node<String, String>> generateNodesMap(Map<String, String[]> lineMap) {
- // initialise map that stores all the station nodes, with the respective
- // station's name as key
- HashMap<String, Node<String, String>> nodesMap = new HashMap<String, Node<String, String>>(64);
- // iterate through every station line
- for (Entry<String, String[]> pair : lineMap.entrySet()) {
- // retrieve station line
- String line = pair.getKey();
- // retrieve stations
- String[] stations = pair.getValue();
- // declare and initialise a variable to indicate if the current
- // iteration is the first
- boolean first = true;
- // store the node found in the previous iteration to be used in
- // creating an edge
- Node<String, String> prevNode = null;
- // iterate through the stations
- Iterator<String> itr = Arrays.asList(stations).iterator();
- while (itr.hasNext()) {
- // retrieve the next station
- String station = itr.next();
- // retrieve already existing node of the station, or create a
- // new one
- Node<String, String> node;
- if (nodesMap.containsKey(station)) {
- node = nodesMap.get(station);
- } else {
- node = new Node<String, String>(station);
- }
- // check if it is the first iteration
- if (first) {
- // ignore creating an edge between a (non-existent) previous
- // node and tell the subsequent iterations that the first
- // iteration has occurred
- first = false;
- // create edge between current node and previous node
- } else {
- // create a new edge between two nodes
- Edge<String, String> edge = new Edge<String, String>(prevNode, node, line);
- // add the generated edge to the two stations
- prevNode.addEdge(edge);
- node.addEdge(edge);
- }
- // add the station to the nodes map with the key as the
- // station's name
- nodesMap.put(station, node);
- // store the previously modified station
- prevNode = node;
- }
- }
- return nodesMap;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement