1

Is there any way to retreive the keys of a hashmap other then using keyset? I have written the following code , I have a hashmap named map, it includes integer keys and double values :

 Set<Integer> keys = sorted_map.keySet();
 Object[] weel = new Object[keys.size()];
 Iterator it = keys.iterator();
 int l = 0;
 while(it.hasNext())
 {
 weel[l] = it.next();
 }

Now I have an array that includes the keys of my map. but now I need to compare these keys with some integer. for example :

 if(weel[1] == 5) 

but as the weel type is Object, I can not do the above and also I cannot cast it to int. how can I do this? is it possible?

asked Mar 1, 2011 at 10:20
3
  • 1
    I dont want to be picky but you are retrieving the keys by using keySet() anyways, then you are iterating over the keys you have acquired and putting them in an new array, so essentially you're just moving a group of integers from a set to an array and nothing else. From what I can see there shouldnt be any differences anyways... Are you sure that you havent misunderstood the assignment? Commented Mar 1, 2011 at 10:32
  • @posdef has a point, the set is sorted why not just iterate through the set to do the comparison, unless you are going to add values to the array the Sorted set will have the same values in the same order at the resulting array. Commented Mar 1, 2011 at 10:40
  • Also be wary that you only have one '=' sign in your if statement, I assume this is a typo but that will just assign 5 to weel (also I don't think it will compile since the assignment operator returns the value of the assignment so the statement would be the equivalent of typing if(5) which works in C but not Java. Commented Mar 1, 2011 at 10:42

4 Answers 4

6

Not sure why you want to create a copy of the keys give you can just use the keys.

You can do

List<Integer> keys = new ArrayList<Integer>(sorted_map.keySet());
if (kyes.get(1) == 5)

or

Integer[] keys = (Integer[]) sorted_map.keySet()
 .toArray(new Integer[sorted_map.size()]);
if (kyes[1] == 5)

or

Iterator<Integer> iter = sorted_map.keySet().iterator();
iter.next(); // skip the first.
if(iter.next() == 5);
answered Mar 1, 2011 at 10:32
Sign up to request clarification or add additional context in comments.

Comments

3

Why don't you declare your array as an Integer[] or int[] instead? In the latter case, you can even use == for comparison. However, you also implicitly use unboxing in that case, which might affect performance if you have a huge map.

answered Mar 1, 2011 at 10:23

2 Comments

I tried it, but it cause error as I cannot put objects in an array of integer
have a look at the intValue() method of the Integer class.
1

Your keyset is of type Integer (not int) as you have correctly denoted with Set. So what you are getting out of the array are objects of type Integer, which cannot be cast to an int. What you should do is create an array of type Integer rather than Object, then when you do your comparison you can use the intValue() method of Integer to get it as an int.

answered Mar 1, 2011 at 10:28

1 Comment

After Java 5 Java will do this automatically it's called autoboxing: download.oracle.com/javase/1.5.0/docs/guide/language/…
0

Firstly you don't need to use an iterator to populate the array you can call the .toArray() method on the keyset like so:

sortedMap.keySet().toArray();

Then all you have to do is cast each element of the object array to an integer as you use it (Or you could cast the array to an integer array when creating it)

if (((Integer)weel[1]0 == 5){
}

Also you might want to note that your if statment only contains one = sign thats and assignment not a comparison.

EDIT:

On reflection you can also you the toArray Method that actually returns a typed array:

Set<Integer> keySet = new sortedMap.keySet(); 
Integer[] weel = {};
weel = keySet.toArray(weel);

you will then have an array of integers which you can compare to any other integer.

answered Mar 1, 2011 at 10:30

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.