Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

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?

share|improve this question
5  
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. – Boris the Spider Jun 26 '15 at 7:00

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());
        }
}
share|improve this answer
1  
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. – Boris the Spider Jun 26 '15 at 7:01
    
Ok, I know your issue. Could you up the code which process the input data? – codeaholicguy Jun 26 '15 at 7:09

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).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.