martin martin - 5 months ago 30
Java Question

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:

Mike-> 2

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download