read text file and store in hashmap. Then sort in order

喜夏-厌秋 提交于 2019-12-04 20:43:24
onon15

Yes, use HashMap<String,Double> and when putting in the values, convert them to double using Double.parseDouble().

(You can do the same with Float rather than Double, but using Double makes so much more sense usually).

The HashMap does not guarantee order:

This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

This would mean that sorting your hash map would be useless. If you need to load key value pairs like those, you could consider implementing your own class which has the required fields and implements the Comparible interface.

This will allow you to load your objects and then just call Collections.sort() to sort the list of custom objects.

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