0

We are adding key value pairs in a HashMap by reading properties file.

While putting key value pair, the key is in Arabic language(Arabic Text) and Value is in English. In debug mode we can see the key value pair in the HashMap. But when we are trying to get values for Arabic key, we are getting null value. We even tried to use containsKey, but its also returning false.

We have defined property as button.fetch= \u062c\u0644\u0628 and putting it into the HashMap as

MessageResources resources = getResources(request, mrc[i].getKey());
Iterator itrEntries = mapKeyMethod.entrySet().iterator();
while (itrEntries.hasNext()) {
    Map.Entry objEntry = (Map.Entry) itrEntries.next();
    String strCaptionKey = (String) objEntry.getKey();
    String strMethodName = (String) objEntry.getValue();
    String strCaption = resources.getMessage(userLocale, strCaptionKey);
    if ((strCaption != null) && !mapCaptionMethod.containsKey(strCaption)) {
        mapCaptionMethod.put(strCaption, strMethodName);
    }
}

Here value of strCaption is Arabic text. And when we try to get value with this key from map, it returns null.

Tom
  • 16,842
  • 17
  • 45
  • 54
  • How do you expect any help if you do not show any code? – fge Oct 05 '15 at 07:24
  • 3
    Have you tried comparing your present and desired key via `equals` to ensure equality? – Smutje Oct 05 '15 at 07:29
  • Did you compare the keys that exist in the map with the ones that you are passing ? Are they equal ? – Saif Asif Oct 05 '15 at 07:30
  • Please add your code and the property file to give us an impression where the problem could come from. Thank you. – Leonid Glanz Oct 05 '15 at 07:30
  • I would suppose your keys are not `String` but some other class. That class needs to have `equals` and `hashCode` properly overridden. – RealSkeptic Oct 05 '15 at 07:30
  • Keep in mind that a mere optical check (i.e. the strings "look" identical) doesn't work for debugging. You'd need to check each character since when looking at a string you might miss spaces or have characters that look similar but are not exactly the same - that might be even more the case with "exotic" (i.e. non-ascii) characters. – Thomas Oct 05 '15 at 07:31
  • There is no notion of language, you have a String and encoding... make sure you do those right... – vach Oct 05 '15 at 07:41
  • 1
    When dealing with unicode is it possible for Strings to look the same but not be the same e.g. unicode 202e which is used with Arabic has zero width so you can't see it. You can even use it in Java variable names o_O. – Peter Lawrey Oct 05 '15 at 10:23
  • I have added code snippet in the summary above. Kindly check and revert back. – Nikhil Naik Oct 06 '15 at 09:37
  • First: please avoid [raw types](http://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) and secondly: you might want to use the same encoding. If your properties file is encoded in UTF-8, then your strings should be too. – Tom Oct 06 '15 at 09:42
  • The encoding is same at all places. – Nikhil Naik Oct 06 '15 at 09:45
  • Try **java.text.Normalizer** to normalize the Unicode sequence to a fixed variant. – Joop Eggen Oct 06 '15 at 09:47
  • I just tried and can confirm HashMaps works with those keys, you problem must be by creating or re-creating/reading the key, not the hashmap: Try yourself: `String k="\u062c\u0644\u0628"; Map m = new HashMap(); m.put(k, "value"); System.out.println("contains:" + m.get(k));` – eckes Oct 06 '15 at 09:51

0 Answers0