I have a hashtable named table
. The type value is long
. I am getting values using .values()
. Now I want to access these values.
Collection val = table.values();
Iterator itr = val.iterator();
long a = (long)itr.next();
But when I try to get it, it gives me error because I can't convert from type object
to long
. How can I go around it?
Number
class can be used for to overcome numeric data-type casting.In this case the following code might be used:
I've prepared the examples below:
Object
tolong
example - 1Console output would be:
Object
todouble
example - 2Console output would be:
Object
todouble
example - 3Be careful about this simple mistake! If a float value is converted by using
doubleValue()
function, the first value might not be equal to final value.As shown below
0.11
!=0.10999999940395355
.Console output would be:
Object
tofloat
example - 4Console output would be: