Home arrow static arrow Java Programming [Archive] - Map with ordered values?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Map with ordered values?
This topic has 6 replies on 1 page.

Posts:187
Registered: 12/15/01
Map with ordered values?  
Aug 2, 2004 2:17 PM



 
hi,

is there any collection with a functionality like HashMap which values (not keys) are kept in a natural order (so i can iterate from highest to lowest value)??

e.g. pseudo:
magicmap.(Collections.reversOrder);magicmap.put(A,7);magicmap.put(B,13);magicmap.put(C,0); while(magicmap.keySet.iterator.hasNext)printout.next


printout:
B,A,C

any ideas?
thanx
 

Posts:37,103
Registered: 3/30/99
Re: Map with ordered values?  
Aug 2, 2004 2:32 PM (reply 1 of 6)



 
Not in the standard API. Check sourceforge or jakarta. Or use google to search these forums--I think this and similar questions are asked a lot.

Or you can just roll your own.

From what you've shown, though, it looks like you just have the keys & values reversed in your map. Will you ever be doing map.get(A)?
 

Posts:187
Registered: 12/15/01
Re: Map with ordered values?  
Aug 2, 2004 2:45 PM (reply 2 of 6)



 
Will you ever be doing map.get(A)?
not at the moment - would there be an easy way?

thanx a lot.
 

Posts:37,103
Registered: 3/30/99
Re: Map with ordered values?  
Aug 2, 2004 2:48 PM (reply 3 of 6)



 
Will you ever be doing map.get(A)?
not at the moment - would there be an easy way?

thanx a lot.

Easy way to do what?

If you're not going to do any gets by A, B, C, then there's no reason to make them keys. Make the integers the keys. If those ints aren't too sparse, then an array might be preferable to a map.

Can you explain what you're using this map for, and why you've chosen the keys and values as you have?
 

Posts:5,965
Registered: 5/17/03
Re: Map with ordered values?  
Aug 2, 2004 8:29 PM (reply 4 of 6)



 
You can use a HashMap and an ArrayList. You add the values also at the end of the ArrayList. Before you traverse the ArrayList you sort it (if you have inserted new values since the last time).

If you have no duplicat values you can use a TreeSet instead. It will automatically keep the values sorted at all times so you won't have to sort.
 

Posts:2,830
Registered: 9/1/03
Re: Map with ordered values?  
Aug 2, 2004 8:37 PM (reply 5 of 6)



 
Not in the standard API.

yes there is isn't there ?

SortedMap ? I have a vauge memory of one ...
 

Posts:2,830
Registered: 9/1/03
Re: Map with ordered values?  
Aug 2, 2004 8:38 PM (reply 6 of 6)



 
Not in the standard API.

yes there is isn't there ?

SortedMap ? I have a vauge memory of one ...

Oh, values not keys.
 
This topic has 6 replies on 1 page.