Solved

cannot cast Integer to Long

Posted on 2014-04-20
2
734 Views
Last Modified: 2014-04-21
below json data that is coming from the business layer.these values are coming dynamically from db.

when  iterate the json and cast it to Long ,getting classcastexception for value 261031668.

"cannot cast Integer to Long".
{Cluster1=4560722019, Cluster2=8796913840, Cluster3=4562452584, Cluster4=4557194248, Cluster5=261031668}

innerEntry.getValue()...4560722019
innerEntry.getValue()...8796913840
innerEntry.getValue()...4562452584
innerEntry.getValue()...4557194248
innerEntry.getValue()...261031668

Open in new window

when we get this kind of big range values what is the generic type we need to use for innerMap in this case.as you can see i have taken innerMap<String,Long>.
 
       
      
                for(Entry<String, Long> innerEntry : innerMap.entrySet()) {
                     System.out.println("innerEntry.getValue()..."+innerEntry.getValue());

                    Long sizeObj = (Long)innerEntry.getValue(); //here getting classcastexception for value 261031668

Open in new window

0
Comment
Question by:chaitu chaitu
2 Comments
 
LVL 26

Accepted Solution

by:
dpearson earned 500 total points
Comment Utility
If you're going to sometimes receive a Long and sometimes receive an Integer then you can consider using Number:

innerMap<String,Number>

Since Number is a base class for both Integer and Long the assignment will work for both Integer and Longs:

Number sizeObj = (Number)innerEntry.getValue() ;

and then when you wish to extract the values from the map you can use longValue() which will be valid for integers and longs:

public long getLongValue(String key) {
   Number lookupValue = innerMap.get(key) ;
   return lookupValue.longValue() ;
}

Doug
0
 
LVL 20

Author Comment

by:chaitu chaitu
Comment Utility
excellent doug.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

This is about my first experience with programming Arduino.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now