tags:

views:

224

answers:

6
Iterator it = myHashMap.keySet().iterator();
while (it.hasNext()) {
   int next = it.next();
}

That doesn't work because it.next() returns Object. My hashmap uses ints for keys. All of my methods accept ints to access the hashmap. How can I actually get an int value when looping through my keys so I can pass it to my other methods?

+2  A: 

First of all try to use Generics while defining your Map/HashMap. Then you don't need to worry.

Second thing, HashMap doesn't use primitive type for keys. Which means you are mistaken, and the actual type used as keys is Integer, not int.

A quick fix can be a cast to int, like below

int next = ((Integer) it.next()).intValue();

A detailed example, with a new loop syntax (introduced in Java 5).

Map<Integer, String> map = new HashMap<Integer, String>();
...
for(int n : map.keySet()) {
  ...
}
Adeel Ansari
Actually casting to int does not work. It says cannot cast from object to int. I also couldn't use Integer.intValue(). I don't know what type of Object it's giving me. :\
Joren
Note: the quick fix above will only work with Java 5 or above. For lesser you need `((Integer) it.next()).intValue()`.
Adeel Ansari
@Joren, just finished to realize, and received your comment. Thanks, fixed it.
Adeel Ansari
+2  A: 

You should use Java generics:

Map<Integer, Object> myHashMap = new HashMap<Integer, Object>();
// ...
for (int key : myHashMap.keySet()) {
    // ...
}
Chris Jester-Young
+1  A: 

HashMaps in Java can only use objects, not primtives.

So it is more likely that your keys are of type Integer, and that when you put stuff in, it gets promoted from int to Integer.

All that being said, this is really old school Java. Your collection should define the key and target type using generics, and there's also a for-each in Java for(Integer key: myHashMap.keySet())

BTW: be aware that there are no guarantees about the order in which you'll be iterating.

Uri
+1  A: 

Your ints are autoboxed into Integers when you use them as keys in Maps.

As any Java programmer knows, you can’t put an int (or other primitive value) into a collection. Collections can only hold object references, so you have to box primitive values into the appropriate wrapper class (which is Integer in the case of int). When you take the object out of the collection, you get the Integer that you put in; if you need an int, you must unbox the Integer using the intValue method. All of this boxing and unboxing is a pain, and clutters up your code. The autoboxing and unboxing feature automates the process, eliminating the pain and the clutter.

akf
+1  A: 

You should use generics.

 Map<Integer, Object> myHashMap;

This gives you keys that are Integer (not int, but that cannot be helped). Integer can be automatically unboxed:

for (int key : myHashMap.keySet()){

}

If you want your keys in ascending order, consider using TreeMap instead of HashMap.

Thilo
Thanks. This (and all the replies below that are similar) are exactly what I needed.
Joren
A: 

I'm not sure if your key array is all you need, but I typically loop through maps like this:

Map<Integer, Object> map = new HashMap<Integer, Object>();

for(Map.Entry<Integer, Object> entry : map.entrySet()) {
    System.out.println("Key: " + entry.getKey());
    System.out.println("Value: " + entry.getValue());
}
Droo