Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorted hashmap iteration [duplicate]

Tags:

java

I have such code:

HashMap<Long, TempPosition> positions = getTempPositions();
    for (SortedMap.Entry<Long, TempPosition> p: positions.entrySet()) {...}

The problem is the 'positions' is not sorted or non valid sorted. What the easiest way to iterate through the hashmap and save its current order?

Thank you

like image 566
nKognito Avatar asked Oct 26 '11 07:10

nKognito


1 Answers

A HashMap by definition doesn't have an order. If you need to preserve or create some kind of order you need to use TreeMap instead of HashMap.

like image 145
Till Helge Avatar answered Sep 16 '22 23:09

Till Helge