简体   繁体   中英

Does this guarantee access the first element of the map?

Map<String, Integer> map = new HashMap<String, Integer>;

...

Map<String, Integer> sortedMap = sortByValue(map);
Integer max = sortedMap.keySet().iterator().next();

What I need is to fetch the first element in the map, which is the max element after sorted.

Edited: The sorted map looks like:

John->100
   Mary->50
   Mike-> 2
   ...

While there are some Map implementations that have a notion of order, this is not part of the Map contract. In particular HashMap does not guarantee any ordering of the entries.

It depends what you did here in sortByValue and if the map that is returned preseves some order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM