0

I have a HashMap for the alphabet that goes

HashMap<Character, Integer> alphabet = new HashMap<>();
   alphabet.put('A', 0);
   alphabet.put('B', 1);
   alphabet.put('C', 2);
   alphabet.put('D', 3);
   //And so on...

I'm trying to ket the key from the int value but am getting this error: The method getKey(int) is undefined for the type HashMap<Character,Integer>

Here's my code:

import java.util.Map.Entry; 
import java.util.HashMap; 
    
    String output = "";

   for(int i = 0; i < message.length(); i++){
        char letter = message.charAt(i);
        int n = alphabet.get(letter);
        int newIndex = Math.floorMod((n + k), 26);
        output += alphabet.getKey(newIndex);
   }
   System.out.println(output + "THE END");

I tried compiling it and got a "Cannot find symbol error" for the getKey function. Any idea what's causing this?

Thanks

redfox
  • 21
  • 3
  • There's no such method, which is why the compiler tells you there's no such method. [Documentation](https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html). Maps look up a **key** and give you a **value**, not the other way around. If you want to find a value, then you need to iterate over the entire map. But the logic of what you're trying to do escapes me. It looks sort of like a caesar cipher, in which case no data structure is needed. – access violation Oct 25 '22 at 03:14
  • 1
    If you know you have a letter, `Character.toUpperCase(c) - 'A'` will give you the index of that letter (0-25). If you don't know that, then you're going to get back `null` from your map lookup, and then your code will blow up when you try to use that as a numeric value. – CryptoFool Oct 25 '22 at 03:27
  • If you are allowed to use other things than standard Java, you can use bidirectional maps like [BidiMap](https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/BidiMap.html) from Apache Commons Collections. This `BidiMap` has a method `getKey`. – csalmhof Oct 25 '22 at 06:31

1 Answers1

0

Get Key from value in Map using Java 8 streams

As there is no pre-defined utility in Map to find the key from value, you can try the below approach for it.

Logic:

Here, I have created getKey method in which you can have map and valueToSearch as an arguments and using the stream and filter function , i have filtered the map based on the value and findFirst will give the desired output as shown in the code below.

In your case, valueToSearch = newIndex.

Code:

private static Character getKey(Map<Character, Integer> alphabet, 
                                int valueToSearch) {
    Character output = null;
    Optional<Character> optional =
            alphabet.entrySet().stream()
                    .filter(x -> x.getValue() == valueToSearch)
                    .map(Map.Entry::getKey).findFirst();
    if(optional.isPresent()){
        output = optional.get();
    }
    return output;
}

TestCases:

I/p: 3
O/p: D

I/p: 2
O/p: C
GD07
  • 1,117
  • 1
  • 7
  • 9