URGENT - plz help - Duplicate Keys in Hashmap :(

A

alisyedm

Hi,

I am loading a hashmap with some values (Strings of the format xxx:xxx
) - and the keys are also strings and they are also of the same format
(xxx:xxx) where x can be any alphanumeric character.

Now when I print out the values, I find multiple rows with duplicated
key values....what could that be ? Please help!

Here is something to show what I am doing to get the values:

Iterator it = myHMap.keySet().iterator();
while (it.hasNext()) {
String key = (String) it.next();
String value= (String) myHMap.get(key);
System.out.println("Key: " key + " - Value: " + value);
}


Isn't that as long as I am giving the same key to load the hashmap, it
should overwrite previous values stored with the same key? ( if any)
 
A

Ali

I know that ideally we need to override equals() and hashcode() but
do I need to do that for simple "String" Keys as well ?

I am assuming thats not the case? Please help!
 
R

Ryan Stewart

Hi,

I am loading a hashmap with some values (Strings of the format xxx:xxx
) - and the keys are also strings and they are also of the same format
(xxx:xxx) where x can be any alphanumeric character.

Now when I print out the values, I find multiple rows with duplicated
key values....what could that be ? Please help!

Here is something to show what I am doing to get the values:

Iterator it = myHMap.keySet().iterator();
while (it.hasNext()) {
String key = (String) it.next();
String value= (String) myHMap.get(key);
System.out.println("Key: " key + " - Value: " + value);
}

Isn't that as long as I am giving the same key to load the hashmap, it
should overwrite previous values stored with the same key? ( if any)
If "myHMap" is a java.util.HashMap, and if you are not granting unsynchronized
multithreaded access to it, then it can't have the same key multiple times. Are
you sure the Strings you're seeing are identical? Identical casing? No left or
right spaces?
 
R

Ryan Stewart

Ali said:
I know that ideally we need to override equals() and hashcode() but
do I need to do that for simple "String" Keys as well ?

I am assuming thats not the case? Please help!
It's already done on String.
 
A

Ali

Ryan,

Thanks for your assertion to my beleif. I am not sure why is this
happening. I have made sure that the values are the same....

Following is the output from the hashmap - the first value is the
"Value" in the hashmap- while the key is made up as "BB:2" (2nd and
3rd values separated by a colon) - then dumped by the code specified
above.

You can see repeated BB & 2 (Keys)

115181,BB,2
115699,BB,2
104192,BB,2
115864,BB,2
101935,BB,2
113982,BB,8
94381,BB,2
84048,BB,11
108523,BB,2
116299,BB,2
81981,BB,2
100535,BB,2
112157,BB,2
96549,BB,2
101936,BB,2
110746,BB,2
95637,BB,2
98399,BB,2
108877,BB,2
82188,BB,2
108522,BB,2
115182,BB,2
101934,BB,2
115183,BB,2
99758,BB,2
113584,BB,2


Not saying that it can't be my mistake - but I have reviwed it again n
again - can't seem to figure it out :-(
 
J

Joona I Palaste

Ali said:
Thanks for your assertion to my beleif. I am not sure why is this
happening. I have made sure that the values are the same....
Following is the output from the hashmap - the first value is the
"Value" in the hashmap- while the key is made up as "BB:2" (2nd and
3rd values separated by a colon) - then dumped by the code specified
above.
You can see repeated BB & 2 (Keys)

Not saying that it can't be my mistake - but I have reviwed it again n
again - can't seem to figure it out :-(

Please post the exact code you have used to generate this output. It
does not appear to be the same output as your original code generates.
Also you could try writing some text (for example "[") before each
line and some text (for example "]") after it, to make any leading or
trailing whitespace stand out.
 
A

Ali

[ This is the code that constructs the hashmap ]

private String getSymbolId(String Symbol, String SymbolTypeId) throws
UpdateFailure{

String SymbolId = null;

Statement stmt = null;
ResultSet rst = null;

//If the Symbol is way too long, just treat it as garbage.
if (Symbol.length() >= maxSymbolLength){
logger.print(50,"getSymbolId: Rejecting invalid Symbol
length: " + Symbol);
return null;
}
if (myHMap == null) {
myHMap = new HashMap()
}


Symbol = Utils.cleanString(Symbol); // remove spaces - garbage
characters etc
if (SymbolTypeId == null || "".equals(SymbolTypeId) ) {
//enforece default type if a type is not valid
SymbolTypeId = getIdForSymbolType("default");
}
String lookupKey = Symbol.trim() +":" + SymbolTypeId.trim();
String value = (String) myHMap.get(Symbol+":"+SymbolTypeId);
if (value != null){ //If something was found ....
String id = ""+value.substring(0,value.indexOf(":"));
return id;
}

// Generate a new Symbol id (idmaker)
SymbolId = ""+idMaker.getSymbolId(); //Just sends a sequence
number - first column in the output )
// Save it for later dump
myHMap.put(Symbol.trim()+":"+SymbolTypeId.trim(),
SymbolId.trim() + ":" + SymbolTypeId.trim());
return SymbolId;
}


---------------------------------------------------------------

[ Code that prints the contents ]


public void dumpHashMap() {


Iterator it = myHMap.keySet().iterator();
//System.out.println(it.toString());
while (it.hasNext()) {
String key = (String) it.next();
String symbol = key.substring(0,key.indexOf(":"));

if (symbol == null || "".equals(symbol ) || symbol.length() <=
0 ) // Very unlikely to happen - old code lefthere- but please note
that duplicates do not contain "NOSYMB_"
symbol = "NOSYMB_" + System.currentTimeMillis();

String value= (String) myHMap.get(key);
if (value != null){ //If something was found ....
String id = value.substring(0,value.indexOf(":"));
String tId = value.substring((value.indexOf(":")+1));
dumpWriter.println(id + "," + ticker + "," + tId);
}
} // end of while

}
 
K

kjc

Ali said:
Ryan,

Thanks for your assertion to my beleif. I am not sure why is this
happening. I have made sure that the values are the same....

Following is the output from the hashmap - the first value is the
"Value" in the hashmap- while the key is made up as "BB:2" (2nd and
3rd values separated by a colon) - then dumped by the code specified
above.

You can see repeated BB & 2 (Keys)

115181,BB,2
115699,BB,2
104192,BB,2
115864,BB,2
101935,BB,2
113982,BB,8
94381,BB,2
84048,BB,11
108523,BB,2
116299,BB,2
81981,BB,2
100535,BB,2
112157,BB,2
96549,BB,2
101936,BB,2
110746,BB,2
95637,BB,2
98399,BB,2
108877,BB,2
82188,BB,2
108522,BB,2
115182,BB,2
101934,BB,2
115183,BB,2
99758,BB,2
113584,BB,2


Not saying that it can't be my mistake - but I have reviwed it again n
again - can't seem to figure it out :-(
Make sure you do a trim() on the keys before you use them in the put.
 
J

John C. Bollinger

Ali said:
I know that ideally we need to override equals() and hashcode() but
do I need to do that for simple "String" Keys as well ?

Often you do *not* need to override equals() and hashCode(). If you
choose to override equals(), however, then you generally must also
override hashCode() to ensure that objects that share an equals()
relationship have the same hashCode(). This is a better-than-poor
practices issue, not an actual language requirement. If you override
equals() but not hashCode() on a class then instances will not be
compatible with hash-based data structures such as HashSets and HashMaps.

The String class provides suitable, consistent implementations of
equals() and hashCode(), and Strings make excellent Map keys for a
variety of reasons. You do not need to -- and indeed cannot -- override
String.equals() or String.hashCode().
 
A

Ali

John,

I agree. But can you help me figure out what is it that I am missing
in this code here? How come I get duplicates ? :-(
 
R

Ryan Stewart

[...]
dumpWriter.println(id + "," + ticker + "," + tId);
[...]
What is "ticker"? It isn't declared anywhere in the code you gave us. This is
one of a few questionables I see, but likely the one causing you problems just
now.
 
J

John C. Bollinger

Ali said:
I agree. But can you help me figure out what is it that I am missing
in this code here? How come I get duplicates ? :-(

You don't. You are not seeing what you think you're seeing.

Exactly what you _are_ seeing I can't say, for you have posted one dump
and two incomplete code fragments, none of which match. Some
possibilities: non-printable characters and/or leading or trailing
whitespace in your keys may make some of them appear the same when they
really aren't; you may be using mixed-case keys but converting case for
comparison or comparing case insensitively; you may be swapping key and
value when you insert the map entries (or when you remove them).
 
M

Matt Humphrey

Ali said:
[ This is the code that constructs the hashmap ]

private String getSymbolId(String Symbol, String SymbolTypeId) throws
UpdateFailure{

String SymbolId = null;

Statement stmt = null;
ResultSet rst = null;

//If the Symbol is way too long, just treat it as garbage.
if (Symbol.length() >= maxSymbolLength){
logger.print(50,"getSymbolId: Rejecting invalid Symbol
length: " + Symbol);
return null;
}
if (myHMap == null) {
myHMap = new HashMap()
}


Symbol = Utils.cleanString(Symbol); // remove spaces - garbage
characters etc
if (SymbolTypeId == null || "".equals(SymbolTypeId) ) {
//enforece default type if a type is not valid
SymbolTypeId = getIdForSymbolType("default");
}
String lookupKey = Symbol.trim() +":" + SymbolTypeId.trim();
String value = (String) myHMap.get(Symbol+":"+SymbolTypeId);
if (value != null){ //If something was found ....
String id = ""+value.substring(0,value.indexOf(":"));
return id;
}

// Generate a new Symbol id (idmaker)
SymbolId = ""+idMaker.getSymbolId(); //Just sends a sequence
number - first column in the output )
// Save it for later dump
myHMap.put(Symbol.trim()+":"+SymbolTypeId.trim(),
SymbolId.trim() + ":" + SymbolTypeId.trim());

Key and value are not the same here. Cannot see what the key really is.
return SymbolId;
}


---------------------------------------------------------------

[ Code that prints the contents ]


public void dumpHashMap() {


Iterator it = myHMap.keySet().iterator();
//System.out.println(it.toString());
while (it.hasNext()) {
String key = (String) it.next();
String symbol = key.substring(0,key.indexOf(":"));

if (symbol == null || "".equals(symbol ) || symbol.length() <=
0 ) // Very unlikely to happen - old code lefthere- but please note
that duplicates do not contain "NOSYMB_"
symbol = "NOSYMB_" + System.currentTimeMillis();

String value= (String) myHMap.get(key);
if (value != null){ //If something was found ....
String id = value.substring(0,value.indexOf(":"));
String tId = value.substring((value.indexOf(":")+1));
dumpWriter.println(id + "," + ticker + "," + tId);

You are only printing the values here--not the keys. Also, your keys are
constructed differently from you values. Please show us the real keys, not
your made-up ones.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
Z

Zer0Frequency

Guys,

I figured it out - the problem was with the data. It was a rather
simple issue but you know sometimes it just gets stuck :-s

The issue was that I had to store 3 Strings in one Hash. (2 to be used
as a key combined) and (2 to be combined and stored as Data). Now I
innocently chose : to be my delimeter so the hash would look something
like this: (Note that the first part of key and second part of data
String are the same values)

KEYS DATA
ABCD:1000 MYDATA ROW:ABCD
ABD:1001 MOREDATA:ABD
ACD:1002 ANOTHERONE:ACD

Now it all would have worked out but I missed on the chances of the
data itself containing the delimeter !!! :( How naive of me.

So the duplicate Keys I was finding was because the firstpart of the
key (or second part of the data - same string) had a ":" in it :(

So it would become
BB:1002
BB::1002
BB:::1002

So as you can see the values are "DIFFERENT" from the hashmap's point
of view - but when I would do a SPLIT - I would get BB From all of them
because the split would be occuring at the first ":" which might
still be part of the actual data :(

Anywayy, its fixed now. Thanks to all of you for helping. I appreciate
it.

Laterz,
Zer0frequency
 
Z

Zer0Frequency

Guys,

I figured it out - the problem was with the data. It was a rather
simple issue but you know sometimes it just gets stuck :-s

The issue was that I had to store 3 Strings in one Hash. (2 to be used
as a key combined) and (2 to be combined and stored as Data). Now I
innocently chose : to be my delimeter so the hash would look something
like this: (Note that the first part of key and second part of data
String are the same values)

KEYS DATA
ABCD:1000 MYDATA ROW:ABCD
ABD:1001 MOREDATA:ABD
ACD:1002 ANOTHERONE:ACD

Now it all would have worked out but I missed on the chances of the
data itself containing the delimeter !!! :( How naive of me.

So the duplicate Keys I was finding was because the firstpart of the
key (or second part of the data - same string) had a ":" in it :(

So it would become
BB:1002
BB::1002
BB:::1002

So as you can see the values are "DIFFERENT" from the hashmap's point
of view - but when I would do a SPLIT - I would get BB From all of them
because the split would be occuring at the first ":" which might
still be part of the actual data :(

Anywayy, its fixed now. Thanks to all of you for helping. I appreciate
it.

Laterz,
Zer0frequency
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top