java - How to sort a hash map using key descending order -
how sort hash map using key descending order. please explain example. , how many way sort hash map. please explain in details
hashmaps don't support sorting. store entries in buckets, how see fit, based on hashcode value of keys. fine storing things , looking them afterwards, unsuitable iterating on contents (which apparently want do) because cannot rely on order , iterating on expensive.
try treemap instead. can specify custom comparator reverse of default comparator. in case entries ordered in descending order. collections.reverseorder create such comparator you, can use this:
new treemap<integer, string>(collections.reverseorder());
Comments
Post a Comment