How to sort a LinkedHashMap by its value class's field?

旧城冷巷雨未停 提交于 2019-12-05 10:28:42
CookieOfFortune

When you return int, the range when average-o.average is between -1 and 1 will always return 0.

One solution is simply change your compareTo function to:

return Float.compare(average, o.average);

You're sorting floating point numbers using integers. Integers don't get rounded; they get truncated. Also, given the way you're actually doing the sorting, consider using a TreeHashMap instead.

(and just to nitpick, Java convention uses lowercase for method and variables names)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!