473,403 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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 27892

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Welson Sun | last post by:
Hi, I am new to UML, I would like to know how can represent JAVA's ArrayList and HashMap with UML diagram? How can I represent the element's class in the ArrayList/HashMap? How is the key in the...
1
by: Christian Gollwitzer | last post by:
Hi, I'm trying to loop over the elements in a hashmap of the STL-implementation by SGI. The point is, that because the key/value pair is stored as std::pair in the COntainer, the code becomes...
4
by: David | last post by:
Hi, I have created the following HashMap class. class HashMap: public hash_map<string, string, HashString, HashStringCompare> { public: HashMap(): hash_map<string, string, HashString,...
2
by: xor | last post by:
I'm doing up a school project using java, and am a little new to it (I've worked with other languages for years though). I've seen code posted by the instructor using HashMap like this... ...
6
by: bumrag | last post by:
This is the car dealership object relating to the coursework, there is also a separate object named car that i think i need to link to. The problem is on the addcar() method. Any help would be...
4
by: panos100m | last post by:
Hi these are the conents of my hashmap printing out the entrySet. entrySet1: OrderDate=10/30/2007, entrySet2: Level_0={Item_0={ItemTotal= 3.99, ItemName=test® in, ShipDate=10/31/2007,...
15
by: lbrtchx | last post by:
Hi, ~ I have found myself in need of some code resembling a Hashmap ~ This is easily done in Java this way: ~ import java.util.*; // __ public class JMith00Test{
3
dlite922
by: dlite922 | last post by:
I could have this done in php in three lines of code but in Java here's what I need to do: English Looping through list of words I call a function that returns a HashSet of movie titles that...
1
by: evelina | last post by:
Hello, I need help. I have the following hashmap: HashMap<HashMap<Dimension, Integer>, String> mapList = new HashMap<HashMap<Dimension, Integer>, String>(); I want to extract Dimesion from the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.