23

I'm getting response in this manner:

[{Id=1066276530, Key1=1815401000238}, {Id=1059632250, Key1=1815401000244}]

When I iterate and convert the values into a string, it throws me the error:

java.lang.Long cannot be cast to java.lang.String in Java

 for (Map<String, String> map : leadIds) {
        for (Map.Entry<String, String> entry : map.entrySet()) {
            String applicationNumber = (String) entry.getValue();
        }
}

I want to convert the value into a string. Is there any issue here?

1
  • 7
    It seems that you have coerced the generics in order to force the compiler to think you have a Map<String, String> when you have a Map<String, Long>. Please show the declaration of leadIds. Commented Jun 26, 2015 at 7:00

2 Answers 2

21

Use String.valueOf() instead of casting:

for (Map<String, Long> map : leadIds) {
        for (Map.Entry<String, Long> entry : map.entrySet()) {
            String applicationNumber = String.valueOf(entry.getValue());
        }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Entry<String, String> already has getValue as a String. No amount of adding arbitrary toString calls will change that. The issue is with the generics.
Ok, I know your issue. Could you up the code which process the input data?
5

Because String and Long are completely different types you cannot cast them, but you can use static method String.valueOf(Long value) and backwards Long.valueOf(String value).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.