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 :)
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: -
-
// the 'inverted' map
-
private Map<Integer>, List<String>>= new TreeMap<Integer, List<String>>();
-
...
-
public void add(Integer freq, String key) {
-
List<String> list= map.get(freq);
-
if (list == null) map.put(freq, list= new ArrayList<String>());
-
list.add(key);
-
}
-
That's all there is to it ;-)
kind regards,
Jos
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: -
-
// the 'inverted' map
-
private Map<Integer>, List<String>>= new TreeMap<Integer, List<String>>();
-
...
-
public void add(Integer freq, String key) {
-
List<String> list= map.get(freq);
-
if (list == null) map.put(freq, list= new ArrayList<String>());
-
list.add(key);
-
}
-
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 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
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 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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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"...
|
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...
| |
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:...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
| |
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 ...
| |