0

I am storting the values of SELECT QUERY in a map , with combined month and year (Mon YY) as key. I used order by date in my query. Its working fine, but when i display the value of a map, its in random order.

Is there any way to get the first inserted element first ( like queue)?

  Inserting element
       Dec 2011     REC     332     12      
       Dec 2011     WER     12      12      
       Dec 2012     QA      212     12 

  Displaying value   
            Inner Key :Dec 2012
            QA
            212
            12
            Inner Key :Dec 2011
            REC
            332
            12
            WER
            12
            12

I need the values in ascending order

wolφi
  • 8,091
  • 2
  • 35
  • 64
ahairshi
  • 381
  • 3
  • 18

2 Answers2

3

Use a LinkedHashMap, which preserves insertion order.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks... This thread helps me to know about LinkedHashMap http://stackoverflow.com/questions/2889777/difference-between-hashmap-linkedhashmap-and-sortedmap-in-java – ahairshi Dec 15 '12 at 13:52
  • The javadoc would have helped you as well. – JB Nizet Dec 15 '12 at 13:53
0

You can use a LinkedHashMap or a TreeMap

Bhavik Shah
  • 5,125
  • 3
  • 23
  • 40
  • 1
    No, a TreeMap won't help, unless the key happens to be Comparable and to have a natural order that corresponds to how the data are already sorted. – JB Nizet Dec 15 '12 at 14:28