Get null with the HashMap

Hi all,

I need a help with HashMap. I have a set of classes. In one of them I initialized the HashMap to put the key with the object in a method.

First, the attribute was:

private HashMap hm ;

Then, I did like this:

public void enqueue_Task(Poisson_output e) {

hm = new HashMap();

JobSpecifications t = new JobSpecifications();

t.setPoisson_output(e);

t.setDSpace();

t.setCpu();

t.setMemory();

hm.put(t.getPoisson_output().getSequence(), t);

////hm.putAll(hm);

q.enqueue(t);

}

Then, I wrote this method:

public HashMap getHash(){

return hm;

}

to call it in the other classes, so I will be able to use the HashMap with its values( keys and objects).

After that, and in another class, I initialized the HashMap again;

private HashMap e;

with this constructor:

public PoissonSimulation(int time) {

e = q.getHash();

}

And finally, I use this method to get the object with a specific key:

public JobSpecifications search(double key){

return (JobSpecifications) e.get(key);

}

============================

Thats it. But, the problem is that when I ran the last method I get a null value!

Any help!

[1295 byte] By [MoonSpotLighta] at [2007-11-27 1:11:13]
# 1
Any Help, Please!
MoonSpotLighta at 2007-7-11 23:46:34 > top of Java-index,Java Essentials,Java Programming...
# 2
> Any Help, Please!Christ, you've been waiting for only 10 minutes!It's also amazing to see that with 61 posts and being almost one year at these forums, you still can't find the code-tags button.
prometheuzza at 2007-7-11 23:46:34 > top of Java-index,Java Essentials,Java Programming...
# 3
Hi,You didn't mention that whether you have already override equals() AND hashCode() method.... If you haven't done it, I guess thats you have to, in order to make it work.best of luck :P
SMHasana at 2007-7-11 23:46:34 > top of Java-index,Java Essentials,Java Programming...
# 4
Can you tell me what is 'q'in the lineq.enqueue(t);
Satish_Patila at 2007-7-11 23:46:34 > top of Java-index,Java Essentials,Java Programming...
# 5
No I didn't. Where do you mean I should do this?
MoonSpotLighta at 2007-7-11 23:46:34 > top of Java-index,Java Essentials,Java Programming...
# 6
Hi MoonSpotLight,Please do reply if you understood my previous message which I posted.I'm waiting to hear from you.TY :D
SMHasana at 2007-7-11 23:46:34 > top of Java-index,Java Essentials,Java Programming...
# 7
No I didn't. Where do you mean I should do this?
MoonSpotLighta at 2007-7-11 23:46:34 > top of Java-index,Java Essentials,Java Programming...
# 8
Whatever your class to be used as keys
SMHasana at 2007-7-11 23:46:34 > top of Java-index,Java Essentials,Java Programming...
# 9
Note that you create a new Map everytime you enqueue a task.As a consequence, it is likely that the Map won't contain more than one entry.Is this what you want ?
TimTheEnchantora at 2007-7-11 23:46:34 > top of Java-index,Java Essentials,Java Programming...
# 10

> Whatever your class to be used as keys

you mean the double being used as a key? yeh, make sure you implement equals and hashCode on double

um

@OP: the fact that you're using a double as a key is quite interesting. you must be using java5+ if the above compiles, so why not take advantage of generics? or are you using multiple different types as keys? :-/

georgemca at 2007-7-11 23:46:34 > top of Java-index,Java Essentials,Java Programming...
# 11
I took the HashMap out of the method, but it still the same.Also, I still don't know what is the benifit from the Hashcode since it returns the in value!
MoonSpotLighta at 2007-7-11 23:46:34 > top of Java-index,Java Essentials,Java Programming...
# 12
If it returns null, it means that no map entry corresponds to the given key.So do some debug (e.g. check the content of the map in your search method.)
TimTheEnchantora at 2007-7-11 23:46:34 > top of Java-index,Java Essentials,Java Programming...
# 13
No, Actually there was an entry that should match.But, I don't see the point of using the Hashcode. Can anyone explain?
MoonSpotLighta at 2007-7-11 23:46:34 > top of Java-index,Java Essentials,Java Programming...
# 14
> ...> But, I don't see the point of using the Hashcode. Can> anyone explain?It is explained here in great detail: http://www.javaworld.com/javaworld/jw-01-1999/jw-01-object.html
prometheuzza at 2007-7-11 23:46:34 > top of Java-index,Java Essentials,Java Programming...
# 15

> No, Actually there was an entry that should match.

So modify your search method like this, just to verify...public JobSpecifications search(double key) {

System.out.println("Search for: "+key);

System.out.println("Map contains the following keys:");

for(Iterator it = e.keySet().iterator(); it.hasNext(); ) {

System.out.println(it.next());

}

return (JobSpecifications) e.get(key);

}

> But, I don't see the point of using the Hashcode. Can anyone explain?

There is no point as you are using Doubles as keys.

TimTheEnchantora at 2007-7-21 20:00:52 > top of Java-index,Java Essentials,Java Programming...