java - How do I get an array of keys from a hashmap that aren't type Object? -
iterator = myhashmap.keyset().iterator(); while (it.hasnext()) { int next = it.next(); }
that doesn't work because it.next() returns object. hashmap uses ints keys. of methods accept ints access hashmap. how can int value when looping through keys can pass other methods?
you should use generics.
map<integer, object> myhashmap;
this gives keys integer (not int, cannot helped). integer can automatically unboxed:
for (int key : myhashmap.keyset()){ }
if want keys in ascending order, consider using treemap
instead of hashmap
.
Comments
Post a Comment