Results 1 to 8 of 8
  1. #1
    hjensen is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default 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.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by hjensen View Post
    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:

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

    kind regards,

    Jos

  3. #3
    Alessio is offline Member
    Join Date
    Oct 2010
    Posts
    14
    Rep Power
    0

    Default

    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

  4. #4
    hjensen is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default

    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

  5. #5
    Alessio is offline Member
    Join Date
    Oct 2010
    Posts
    14
    Rep Power
    0

    Default

    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

  6. #6
    hjensen is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default

    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

    Java Code:
    char letter = map.get(keySet(25));
    ?

  7. #7
    Alessio is offline Member
    Join Date
    Oct 2010
    Posts
    14
    Rep Power
    0

    Default

    something along the line of:

    Java 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) 
    					);
    		}

  8. #8
    hjensen is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

Similar Threads

  1. Problems with HashMaps
    By li_bi in forum New To Java
    Replies: 13
    Last Post: 02-24-2010, 12:08 PM
  2. HashMaps get key for a specific value
    By andre1011 in forum Advanced Java
    Replies: 2
    Last Post: 03-11-2009, 02:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •