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?
I faced the same problem but while doing JSP coding. The above mentioned suggestions regarding Long and generics either did not work or did not fit into the code fragment.
I had to solve it like this(in JSP):
and afterwards use my Object y like <%=y%> as we would use any other Java variable in scriptlet.
Try this:
You end up with a Long object but with autoboxing you may use it almost like a primitive long.
Another option is to use Generics:
in my case I have an array of Objects that I get from a flex client,
sometimes the numbers can be interpreted by java as int and sometimes as long,
so to resolve the issue i use the 'toString()' function as follows:
You should use the new Generics features from Java 5.
You can read this quick howto or this more complete tutorial.
Try :
long a = ((Long) itr.next()).longValue();