• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Map Issues

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am using a Hash Map, and i add following values : -

Map map = new HashMap();

map.put("Any","-1");
map.put("Below $100","101");
map.put("$100 - 199","100");
map.put("200 - 299","-200");

when i get the output i get in different order then the order i inserted, any idea how to get teh output in the same order as the insertion.
I also tried to use "LinkedHashMap" i get the same output.
Thanks
aziz
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, a TreeMap is a SortedMap but you'd have to provide your own Comparator to determine the order. That can be a bit tricky and doesn't feel appropriate given your data.

The first thing that sprang to mind is to use a List of Map.Entry. However, that has a strong smell of hack about it and you would only be able to access the entries using the List methods, not the Map methods, i.e. no automatic get value by key look-up.

After a little more thought I think the best solution is to use the OrderedMap implementation (LinkedMap) from Jakarta Commons Collections.

Jules
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am more than a bit mystified by

Originally posted by Aziz Dhanani:
I also tried to use "LinkedHashMap" i get the same output.

Are you sure you did that right, Aziz? My suggestion would be to use LinkedHashMap, because it guarantees to preserve insertion order when iterating through the map. It works for me and I wonder why it didn't seem to work for you.

I wouldn't use OrderedMap if a JDK class will do just as well.

- Peter
[ September 08, 2004: Message edited by: Peter den Haan ]
 
JulianInactive KennedyInactive
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Having read a little further - ahem - if you instantiate a LinkedHashMap thus:

you get the elements returned in access order rather than insertion order. I imagine it's unlikely you're actually doing that. How are you iterating over the Map to determine the order?

Jules
 
You are HERE! The other map is obviously wrong. Better confirm with this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic