-2
    String keys = "3|PNSN--G";   
    System.out.println(keys);
    System.out.println(test.get(keys));
    if ("c"==test.get(keys))
        System.out.println("Sucess");
    else
        System.out.println("Failed");



    3|PNSN--G
    c
    Failed

Why i cant compare it? If "c" is a String and value is a String "c" why it fails?

Ewa Kania
  • 41
  • 1
  • 2
  • 8

2 Answers2

2

You should use .equals() rather than == operator for comparing String contents. == operator will check if both references point to same Object instance or not which is clearly not the case here.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
0

String comparison in Java is just a comparison of the references.

You should use "c".equals(test.get(keys)).

kjaquier
  • 824
  • 4
  • 11