Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems with Java HashMaps and putAll() method
    primarykey
    data
    text
    <p>Hey everybody, I am writing a program that compares the running times of various Dijkstra implementations. I parse the relevant info from a text file and enter it into a hashtable. In my current implementation I have a 'master' hashtable that holds the Node info and that info is copied into a temporary hashtable which is then passed onto different Dijkstra classes. </p> <pre><code>public class Router { public static Map &lt;Integer, Node&gt; nodes = new HashMap&lt;Integer, Node&gt;(); public static Map &lt;Integer, Node&gt; temp = new HashMap&lt;Integer, Node&gt;(); public static int target = 2; public static int start = 0; public static void main(String[] args) throws IOException { Parser p3 = new Parser(args[0], nodes, p); temp.putAll(nodes); // Test 1 DijkstraFib fb = new DijkstraFib(temp, start, target, p); temp.clear(); temp.putAll(nodes); //Test 2 DijkstraBinary d = new DijkstraBinary(temp, start, target, p); temp.clear(); temp.putAll(nodes); // Test 3 Dijkstra b = new Dijkstra(temp, start, target, p); } </code></pre> <p>}</p> <p>So rather then having to invoke a parser three times for each Dijkstra class, I simply want to copy all the values from <code>nodes</code> table to <code>temp</code>, since <code>temp</code> gets modified during the method execution. The first two tests run fine, but the third test fails. If I swap Test 2 and Test 3 around then Test 2 fails; basically, whichever test is run last, fails. Here's my Dijkstra method:</p> <pre><code>void route(Map &lt;Integer, Node&gt; nodes, int source) { Node start = nodes.get(source); start.distance = 0; unsettledNodes.add(start); while(!unsettledNodes.isEmpty()) { Node n = extractMin(); visited.put(n.name, n); relax_neighbours(n, nodes); } } void relax_neighbours(Node n, Map &lt;Integer, Node&gt; nodes) { for (int i = 0; i &lt; n.outgoing.size(); i++) { Edge edge = n.outgoing.get(i); Node v = nodes.get(edge.node); if (isSettled(v)) { continue; } double shortDist = n.distance + edge.length; if (shortDist &lt; v.distance) { unsettledNodes.remove(v); v.distance = shortDist; v.previous = n; unsettledNodes.add(v); } } } </code></pre> <p>and here's my method that prints the path starting from the target node:</p> <pre><code> void print_path(Map &lt;Integer, Node&gt; visited, int i) { ArrayList&lt;Node&gt; path = new ArrayList&lt;Node&gt;(); for (Node target = visited.get(i); target != null; target = target.previous) { path.add(target); } System.out.println("Simple Dijkstra took " + this.time_taken + " ms"); System.out.print("Min dist from " + this.source + " to " + this.target + " = " + path.get(0).distance + " : "); Collections.reverse(path); System.out.print(path.get(0).name); p.append(Integer.toString(path.get(0).name)); for (int k = 1; k &lt; path.size(); k++) { System.out.print(" -&gt; " + path.get(k).name); } System.out.println(); } </code></pre> <p>and here's the console output:</p> <pre><code>Edges: 948464 Fibonacci Dijkstra took 340 ms Min dist from 0 to 2 = 89.0 : 0 -&gt; 195 -&gt; 5523 -&gt; 5504 -&gt; 5870 -&gt; 5835 -&gt; 3076 -&gt; 15319 -&gt; 5588 -&gt; 5911 -&gt; 2 Binary Dijkstra took 302292 ms Min dist from 0 to 2 = 89.0 : 0 -&gt; 195 -&gt; 5523 -&gt; 5504 -&gt; 5870 -&gt; 5835 -&gt; 3076 -&gt; 15319 -&gt; 5588 -&gt; 5911 -&gt; 2 Simple Dijkstra took 0 ms Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:571) at java.util.ArrayList.get(ArrayList.java:349) at Dijkstra.print_path(Dijkstra.java:115) at Dijkstra.&lt;init&gt;(Dijkstra.java:25) at Router.main(Router.java:33) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload