Java sort hashmap by double value not working : <p>I am trying to sort a hashmap containing an object and a double value but it's not working as expected. Here is my code.</p>

<pre><code>HashMap&lt;Route, Double&gt; unsortedRoutes = new HashMap&lt;&gt;();

unsortedRoutes.add(new Route("Route Z"),9.49307305463114167);
unsortedRoutes.add(new Route("Route A"),9.546311415463114167);
unsortedRoutes.add(new Route("Route K"),9.37307305463114167);
unsortedRoutes.add(new Route("Route Y"),9.89730305463114167);
unsortedRoutes.add(new Route("Route Z"),3.49307305463114167);
unsortedRoutes.add(new Route("Route A"),3.41870305463114167);
unsortedRoutes.add(new Route("Route K"),4.49307305463114167);
unsortedRoutes.add(new Route("Route Y"),5.34307305463114167);

HashMap&lt;Route, Double&gt; sortedRoutes = new HashMap&lt;&gt;();
        unsortedRoutes.entrySet()
                .stream()
                .sorted(comparingByValue())
                .forEachOrdered(x -&gt; sortedRoutes.put(x.getKey(), x.getValue()));
</code></pre>

<p>When I ran the code these are the results I get</p>

<pre><code>Name: Route D Distance = 3.4187030546311417
Name: Route J Distance = 5.343073054631142
Name: Route A Distance = 9.546311415463114
Name: Route Y Distance = 9.897303054631141
Name: Route U Distance = 4.493073054631141
Name: Route K Distance = 9.373073054631142
Name: Route Z Distance = 9.493073054631141
Name: Route Q Distance = 3.4930730546311417
</code></pre>

<p>I expected the results to be listed in ascending order.</p>
