我正在尝试对包含一个对象和一个双精度值的哈希图进行排序,但它没有按预期工作。这是我的代码。
HashMap<Route, Double> unsortedRoutes = new HashMap<>();
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<Route, Double> sortedRoutes = new HashMap<>();
unsortedRoutes.entrySet()
.stream()
.sorted(comparingByValue())
.forEachOrdered(x -> sortedRoutes.put(x.getKey(), x.getValue()));
当我运行代码时,这些是我得到的结果
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
我希望结果按升序列出。
aluckdog
相关分类