# Concerning HashMaps

Printable View

• 10-18-2010, 11:25 AM
hjensen
Concerning HashMaps
Hey guys, I am trying to create a program to decrypt a large amount of text without user interaction. I just need to overcome one obstacle and the rest should be smooth sailing.

I have read the file with the encrypted text and I have successfully managed to count the occurrence of each letter. I have stored this data in a (non-sorted) HashMap, and I would like to know the alphabetic position of the letter with the highest frequency. In other words, if the most common letter in the text is "r", the returned value should be either 17 or 18 (depending on whether "a" is assigned 0 or 1).

My first idea was to create a new HashMap with a=0, b=1 etc. but I assume there is a much more elegant and much less demanding solution to my problem. Any pointers would be appreciated, and please tell me if I am not being clear enough. I am not used to this kind of thing.
• 10-18-2010, 12:31 PM
JosAH
Quote:

Originally Posted by hjensen
My first idea was to create a new HashMap with a=0, b=1 etc. but I assume there is a much more elegant and much less demanding solution to my problem. Any pointers would be appreciated, and please tell me if I am not being clear enough. I am not used to this kind of thing.

All you have to do is this:

Code:

```char letter= ...; int result= letter-'a';```
This produces a zero based result assuming only lower case letters.

kind regards,

Jos
• 10-18-2010, 01:33 PM
Alessio
If I understood correctly, what you want to do is to sort a HashMap?

If so, have a look at:
Sort an HashMap - Real's Java How-to

Regards,
Alessio
• 10-18-2010, 02:08 PM
hjensen
I'm sorry guys, looking back at the original post I can see I've done a pretty poor job explaining where exactly I need help.

I have HashMap containing numbers of occurrence of each letter (I've converted all the upper case letters to lower case). I then need the alphabetic position of the letter with the highest frequency. For one particular text it looks like this (after sorting it):

{m=42, k=165, w=242, d=354, i=955, x=1564, o=1635, c=1960, s=2539, z=2873, p=3068, l=3141, j=3316, t=3325, h=4522, y=5767, q=6141, e=6887, f=7931, u=8617, a=9069, v=9440, b=10006, n=10548, g=13085, r=18055}

What I need it to return now is (in this particular example) the number 17, corresponding to r's position in the alphabet. I'm just not quite sure how to get it without user interaction.

I hope it makes more sense now. I have roughly one week of programming experience so I apologize if I seem a little dim on the subject.:p
• 10-18-2010, 02:11 PM
Alessio
well, just sort the HashMap based on the values, get the first entry and cast it to "char"
Then you can apply the code of JosAH, i.e. "subtract" the letter 'a' (and maybe add +1) to the get the position in the alphabet
• 10-18-2010, 02:41 PM
hjensen
Yeah, but how would you go about getting the letter? Everything I've tried gives me the corresponding number of occurrences. How would I pinpoint that I need the last letter (it's sorted ascending) in the HashMap? Can I get a specific value of the keyset in any way? I mean, is there something to the effect of

Code:

`char letter = map.get(keySet(25));`
?
• 10-18-2010, 02:53 PM
Alessio
something along the line of:

Code:

```                final HashMap<Character, Integer> freq = new HashMap<Character, Integer>();                 freq.put('a', 1);                 freq.put('b', 5);                 freq.put('c', 3);                 Character[] keys = new Character[freq.size()];                 freq.keySet().toArray(keys);                                 Arrays.sort(keys);                                 for (Character o : keys) {                         System.out.printf("letter: %s, pos in alphabet: %d, freq: %d\n",                                         o, o -'a', freq.get(o)                                         );                 }```
• 10-18-2010, 03:36 PM
hjensen
Eureka! Thank you so much for your help. :)