Results 1 to 3 of 3
  1. #1
    andre1011 is offline Member
    Join Date
    Feb 2009
    Sacramento CA
    Rep Power

    Default HashMaps get key for a specific value

    Hello I am working on a morse code application using 'HasMap'. The map contains key value pairs which map letters to morse code

    I can get the value for the given key like this => value = map.get(key);

    Now I need to go in the other direction, how can I get the key for a given value? In other words how can I do something like

    key = map.get(value);

    I can translate from English to morse code but not the reverse.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Rep Power


    Well, the easiest way, would be to create two HashMaps.

    Java Code:
    for (Map.Entry<String, String> entry : map.entrySet()) {
      if (values.equals(entry.getValue())) {
        answer = entry.getKey();

  3. #3
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Rep Power


    Definitely create two maps (you can always create yourself a little function that takes "key" and "value" and puts them into both maps). Otherwise, you lose the whole benefit of a hash map.

Similar Threads

  1. Reading specific lines
    By ivvgangadhar in forum New To Java
    Replies: 8
    Last Post: 01-12-2009, 08:53 AM
  2. Either ResultSet or a specific object?
    By saul_2110 in forum JDBC
    Replies: 3
    Last Post: 12-06-2008, 07:41 AM
  3. Replies: 2
    Last Post: 11-08-2008, 10:40 AM
  4. Select specific cell
    By Echilon in forum New To Java
    Replies: 1
    Last Post: 01-01-2008, 07:47 AM
  5. Rejecting connections from a specific IP
    By javaplus in forum Networking
    Replies: 2
    Last Post: 12-21-2007, 02:28 PM

Posting Permissions

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