Java method links are converted on a single line

I need to add the two values together and store them in the HashMap object as long

long total = currentRowContents.get("value_A").longValue() + currentRowContents.get("value_B").longValue();

I guess this won't work, because currentrowcontents is an object of HashMap type, so from currentrowcontents The content returned by get (...) needs to be converted to long type, and then I can use it Longvalue() method

I know I can solve the problem by breaking them into separate statements and making some transformations But I wonder if there is a way to keep the above work from splitting, and if it really needs to be cast (I believe it does), where to put it?

The editor doesn't mean it will change anything, but for those who want to know more, the answer I receive can really solve the problem However, the hash map I use is of object type. Although it is more like string, it does contain data from the database Unfortunately, I can't change the hash map because it comes from a dedicated build framework that I can't change

Solution

It looks like you're using raw type for your map Given the use of longvalue () in your question, it is reasonable to assume that the value of map is of type long

Generics can be used to eliminate the need for casting

Map<String,Long> currentRowContents = new HashMap<String,Long>();

If the source of the map is not under your control, you need to cast it

long total = ((Long)currentRowContents.get("value_A")).longValue() + 
                  ((Long)currentRowContents.get("value_B")).longValue();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>