Connecting Tech Pros Worldwide Forums | Help | Site Map

Sorrting Elements In Map Collection Based On Value

Member
 
Join Date: May 2007
Posts: 72
#1: Nov 27 '07
I need to sort out based on frequency certain events that happen.

I use a map with the event name as the key(String) and the frequency(Integer) as the value and every time an event happens.

I have no problems with the part of adding and updating but am abit stuck at the sorting part as I need to display the events sorted from higest frequency to lowest.

At first I was thinking of putting them into another hashmap with the frequency as the key as it auto sorts based on key but if I have 2 or more events with the same frequency, I might overwrite them.

TIA :)

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Nov 27 '07

re: Sorrting Elements In Map Collection Based On Value


Quote:

Originally Posted by KWSW

I need to sort out based on frequency certain events that happen.

I use a map with the event name as the key(String) and the frequency(Integer) as the value and every time an event happens.

I have no problems with the part of adding and updating but am abit stuck at the sorting part as I need to display the events sorted from higest frequency to lowest.

At first I was thinking of putting them into another hashmap with the frequency as the key as it auto sorts based on key but if I have 2 or more events with the same frequency, I might overwrite them.

TIA :)

So now you have a Map<String, Integer> where the keys are Strings and the
frequencies are Integers.

Build another map like this: Map<Integer, List<String>> and add your value, key
pairs (frequencies, keys) like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. // the 'inverted' map
  3. private Map<Integer>, List<String>>= new TreeMap<Integer, List<String>>();
  4. ...
  5. public void add(Integer freq, String key) {
  6.    List<String> list= map.get(freq);
  7.    if (list == null) map.put(freq, list= new ArrayList<String>());
  8.    list.add(key);
  9. }
  10.  
That's all there is to it ;-)

kind regards,

Jos
Member
 
Join Date: May 2007
Posts: 72
#3: Nov 27 '07

re: Sorrting Elements In Map Collection Based On Value


Quote:

Originally Posted by JosAH

So now you have a Map<String, Integer> where the keys are Strings and the
frequencies are Integers.

Build another map like this: Map<Integer, List<String>> and add your value, key
pairs (frequencies, keys) like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. // the 'inverted' map
  3. private Map<Integer>, List<String>>= new TreeMap<Integer, List<String>>();
  4. ...
  5. public void add(Integer freq, String key) {
  6.    List<String> list= map.get(freq);
  7.    if (list == null) map.put(freq, list= new ArrayList<String>());
  8.    list.add(key);
  9. }
  10.  
That's all there is to it ;-)

kind regards,

Jos

ok will try it out...

btw it will be from the lowest to highest right? Is there anyway to get from the back or sort it the other way around since I need the highest frequency?
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Nov 27 '07

re: Sorrting Elements In Map Collection Based On Value


Quote:

Originally Posted by KWSW

ok will try it out...

btw it will be from the lowest to highest right? Is there anyway to get from the back or sort it the other way around since I need the highest frequency?

Use -frequency for the key values? This is just a quick hack.

kind regards,

Jos
Member
 
Join Date: May 2007
Posts: 72
#5: Nov 27 '07

re: Sorrting Elements In Map Collection Based On Value


Quote:

Originally Posted by JosAH

Use -frequency for the key values? This is just a quick hack.

kind regards,

Jos


thanks again... it works... one last qn(hopefully)...

I know i can use

for(Int i : map.keyset())

to go through the whole map, but if I want to just take out the first ten, is there another type of for loop to do it?
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#6: Nov 27 '07

re: Sorrting Elements In Map Collection Based On Value


Quote:

Originally Posted by KWSW

thanks again... it works... one last qn(hopefully)...

I know i can use

for(Int i : map.keyset())

to go through the whole map, but if I want to just take out the first ten, is there another type of for loop to do it?

Sure, use a counter and when it reaches the value 10 break out of the loop
(there are more ways to skin this cat though).

kind regards,

Jos
Reply