HashMap entries disappear !!

Hey Guys,

This is my code snippet:

HashMap map = null;

try{

-- map = new HashMap();

--connect to DB

while(rs.next()){

// do some calculations...etc..etc..

map.put(x,y);

}

}catch(xxxx)

return map;

} //method closes

On the line map.put(..) I can see that the items do get inserted into the map.

When I do map.size() before return map, it shows size=1 where as map.put was called four times , hence four items were inserted.

Any clues ?

thanks !

AZ

[558 byte] By [azaidi1a] at [2007-11-27 11:20:18]
# 1

I would imagine you are doing something like below. The keys in a hashmap have to be unique or they will be overwritten. So in your code x must the same for all.

import java.util.HashMap;

import java.util.Map;

public class MapTest {

public static void main(String[] args) {

Map<String, String> map = new HashMap<String, String>();

String key = "Hello";

String value = "Good bye";

for (int i = 0; i < 4; i++) {

map.put(key, value); //Try changing to map.put(key+i, value);

System.out.println(map.size());

}

}

}

_helloWorld_a at 2007-7-29 14:41:52 > top of Java-index,Java Essentials,Java Programming...
# 2

Thanks for the reply.

The keys are unique, I suppose.

Some more details:

while(rs.next()){

extract data from DB

do some calculations

if(some conditions arise){

//map.put is called for unique database entires only after the condition is met.

map.put(id,total);

//when I put a print statement here, it gets called 4 times; that means map.put(..) was called four times !

}

} // while closes

}catch(...)

//here the size=1 not 4; it should be 4.

return map;

} //method closes

azaidi1a at 2007-7-29 14:41:52 > top of Java-index,Java Essentials,Java Programming...
# 3

The keys are unique entries from the database. The value is calculated. There is no for(..) loop.

azaidi1a at 2007-7-29 14:41:52 > top of Java-index,Java Essentials,Java Programming...
# 4

Have you tried printing x & y to see what values get printed, you can have one null key in a hashmap and it might be that you are entering null four times, overwriting the previous each time.

_helloWorld_a at 2007-7-29 14:41:52 > top of Java-index,Java Essentials,Java Programming...
# 5

I'd like to post some code:

public HashMap doSearch(..........){

HashMap map = new HashMap();

try{

--do DB connections

while(rs.next()) {

-do some stuff for each database entry --

if(foundA==true ||foundB==true || foundC==true ){

count_A = adjuster(COUNT_A);

count_B = adjuster(COUNT_B);

count_C = adjuster(COUNT_C);

total = count_A+count_B+count_C ;

if(and==true && total!=3.0){

map.put(itemID,total);

System.out.println(""); // this gets called 4 times !

COUNT_A=0.0;

COUNT_B=0.0;

COUNT_C=0.0;

countMatch=0;

} // inside if closed for this item

}total=0.0; // outside if closes for this item

} // while closed

}catch(SQLException sqlex){} //try-catch closed

return map; // this shows map.size()=1 !! it should be 4 !!

} // search method closes

azaidi1a at 2007-7-29 14:41:52 > top of Java-index,Java Essentials,Java Programming...
# 6

X & Y in map.put(...) ?

X = value from DB its an int; there are no null values in DB, otherwise there'd be an SQL exception.

Y = value calculated in algorithm its a double.

azaidi1a at 2007-7-29 14:41:52 > top of Java-index,Java Essentials,Java Programming...
# 7

> X & Y in map.put(...) ?

>

> X = value from DB its an int; there are no null

> values in DB, otherwise there'd be an SQL exception.

Thats not correct.

Well can you print ItemID instead of printing fresh air and we might be able to solve your problem.

Use Code tags when posting code.

http://forum.java.sun.com/help.jspa?sec=formatting

_helloWorld_a at 2007-7-29 14:41:52 > top of Java-index,Java Essentials,Java Programming...
# 8

Thanks. I got it. The itemId was 0.

azaidi1a at 2007-7-29 14:41:52 > top of Java-index,Java Essentials,Java Programming...
# 9

> Thanks. I got it. The itemId was 0.

Also you can return null from a DB like any other value.

_helloWorld_a at 2007-7-29 14:41:52 > top of Java-index,Java Essentials,Java Programming...
# 10

The exact problem was that the itemID that I was inserting into the map was 0 and it was being overwritten again and again. It was typo !!

thanks a ton !

AZ

azaidi1a at 2007-7-29 14:41:52 > top of Java-index,Java Essentials,Java Programming...
# 11

> The exact problem was that the itemID that I was

> inserting into the map was 0 and it was being

> overwritten again and again. It was typo !!

Told you so :P

> thanks a ton !

>

> AZ

Np.

_helloWorld_a at 2007-7-29 14:41:52 > top of Java-index,Java Essentials,Java Programming...