470,849 Members | 1,142 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

HashMap Question

I have a HashMap that is storing form data that will later be inserted
into a database. I have been able to create the HashMap just fine, but
I wanted to be able to take my HashMap and just "dump" it out to the
screen to make sure that everything is working as I expect. (It was
really to easy to code so I think that I may be mising something).

This is a piece of the code where I am updating the HashMap:
if ( map.containsKey(temp))
{
// get MyData
MyDataTmp = (DSNdata)map.get(temp);
if( tempAction.equalsIgnoreCase("alias"))
{
MyDataTmp.putAlias("Y");
}
else if( tempAction.equalsIgnoreCase("load"))
{
MyDataTmp.putLoad("Y");
}
// delete the key and re add it
map.remove(temp);
map.put(temp, MyDataTmp);

}

This is where I am trying to "print" the hashmap:
out.println(map.size());
// Iterate over the keys in the map
Iterator it = map.keySet().iterator();
while (it.hasNext()) {
// Get key
Object Key = it.next();
System.out.println(Key);
System.out.println("Hi");
}

// Iterate over the values in the map
it = map.values().iterator();
while (it.hasNext()) {
// Get value
Object Value = it.next();
System.out.println(Value);
System.out.println("Hi");
}
The println(map.size()) returns a number that is changing based on the
amout of data being processed by the form. This number does change
based on the amount of data that is being sent to it.

Can anyone help here? I added the "Hi" to the loop to see if it even
is going into the loop, and it dosent, so I would assume that the
hasNext() is returning false.

Thanks is advance for help,
Doug

Jul 18 '05 #1
2 27736

First of all, you can always print the contents of any Map by just
using println on the map itself ... you don't have to iterate through
it.

The biggest recommendation I have for you is to get and use a good IDE
with a debugger. That will let you see where you are going wrong. I
and many others strongly prefer IntelliJ IDEA, but it costs money
(after the eval runs out, that is). They have a beta program which
will let you use their software until it is released, but you have to
put up with the fact that they are changing the problem relatively
often.

Eclipse is the other major contender and is free. I last tried it
quite some time ago and got a bad taste. Presumably it is massively
better now.

Jul 18 '05 #2
Two things:
1) if map.containsKey(temp) returns false, where is the code that adds the
object to the HashMap? Is it possible that you never add anything to the
HashMap?

2) You don't need the map.remove() or map.put() to update the HashMap.
map.get() returns a reference to the actual stored object not a Clone of it.
So calling DSNdata.putAlias() and DSNdata.putLoad() already updates the
DSNdata object stored by the HashMap.

<do*****@kc.rr.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
I have a HashMap that is storing form data that will later be inserted
into a database. I have been able to create the HashMap just fine, but
I wanted to be able to take my HashMap and just "dump" it out to the
screen to make sure that everything is working as I expect. (It was
really to easy to code so I think that I may be mising something).

This is a piece of the code where I am updating the HashMap:
if ( map.containsKey(temp))
{
// get MyData
MyDataTmp = (DSNdata)map.get(temp);
if( tempAction.equalsIgnoreCase("alias"))
{
MyDataTmp.putAlias("Y");
}
else if( tempAction.equalsIgnoreCase("load"))
{
MyDataTmp.putLoad("Y");
}
// delete the key and re add it
map.remove(temp);
map.put(temp, MyDataTmp);

}

This is where I am trying to "print" the hashmap:
out.println(map.size());
// Iterate over the keys in the map
Iterator it = map.keySet().iterator();
while (it.hasNext()) {
// Get key
Object Key = it.next();
System.out.println(Key);
System.out.println("Hi");
}

// Iterate over the values in the map
it = map.values().iterator();
while (it.hasNext()) {
// Get value
Object Value = it.next();
System.out.println(Value);
System.out.println("Hi");
}
The println(map.size()) returns a number that is changing based on the
amout of data being processed by the form. This number does change
based on the amount of data that is being sent to it.

Can anyone help here? I added the "Hi" to the loop to see if it even
is going into the loop, and it dosent, so I would assume that the
hasNext() is returning false.

Thanks is advance for help,
Doug

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Welson Sun | last post: by
1 post views Thread by Christian Gollwitzer | last post: by
4 posts views Thread by David | last post: by
2 posts views Thread by xor | last post: by
4 posts views Thread by panos100m | last post: by
15 posts views Thread by lbrtchx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.