473,505 Members | 15,036 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sorrting Elements In Map Collection Based On Value

72 New Member
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 :)
Nov 27 '07 #1
5 1558
JosAH
11,448 Recognized Expert MVP
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
Nov 27 '07 #2
KWSW
72 New Member
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?
Nov 27 '07 #3
JosAH
11,448 Recognized Expert MVP
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
Nov 27 '07 #4
KWSW
72 New Member
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?
Nov 27 '07 #5
JosAH
11,448 Recognized Expert MVP
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
Nov 27 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

8
2188
by: Generic Usenet Account | last post by:
To settle the dispute regarding what happens when an "erase" method is invoked on an STL container (i.e. whether the element is merely removed from the container or whether it also gets deleted in...
1
1606
by: Howard Jess | last post by:
Apparently, form elements of type <input type="image" src="...> are not included in the form's elements collection. I don't understand why not; according to DOM2, all form control elements...
4
27922
by: Logico | last post by:
Hi everybody, I need to do a function in javascript to check or uncheck all checkboxes with the same id. I want this function to work in every form and every page of my site, as I will use the same...
33
2790
by: abs | last post by:
Hi all. My list: <ul> <li id="a" onclick="show(this)">Aaaaaaaa</li> <li id="b" onclick="show(this)">Bbbbbbbb</li> <li id="c" onclick="show(this)">Cccccccc <ul> <li id="d"...
7
3903
by: mittal.pradeep | last post by:
What is the better table design for a data collection application. 1. Vertical model (pk, attributeName, AttributeValue) 2. Custom columns (pk, custom1, custom2, custom3...custom50) Since the...
22
3298
by: Luke | last post by:
Elements with name attribute: form, input, textarea, a, frame, iframe, button, select, map, meta, applet, object, param, img (if you know more reply...) Methods of addresing html elements:...
10
3619
by: Mark C. Neustadt | last post by:
Okay, okay... from what I can find, I'm gonna be out of luck. I also understand that it *shouldn't* matter but it does. I'm trying to send some XML to Amazon and they're requiring the nodes to be...
5
1322
by: Jeff Stewart | last post by:
Let's say I create a collection of DateTime objects like so: Dim clxn_Times As Collection = New Collection() Let's go on to say I add 3 elements to the collection. How do I pull off the...
11
2206
by: shankwheat | last post by:
I have a function which passes text from txtdebt to debtsbox which works fine. However, I want to add code which examines the value of debtsbox and if any of the values the user entered contain the...
0
7216
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7303
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7018
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4699
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1528
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.