Home arrow static arrow Java Programming [Archive] - string equality in a HashMap
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - string equality in a HashMap
This topic has 10 replies on 1 page.

Posts:7
Registered: 10/11/98
string equality in a HashMap  
Aug 4, 2004 8:12 PM



 
Question:
String a = new String(?08001a908000016f?);
String b = new String(?08001a908000016f?);

Will a.equals(b) always be true? This apparently should work, but I run into problems.

I?m using ConcurrentHashMap (one of the open source concurrency utilities from EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap) and I have a hashmap of objects.

I?m using a java String object containing an r_object_id as the key to this HashMap.

It works for most cases. But for a few cases, the r_object_id is not found when it is really in the HashMap. It can?t be a problem with the ConcurrentHashMap utility because I did a small function that walks through the list of keys and does a .equals(missingKey) comparison for each one.

See a code and trace below.

public String debugGetHashMapSummary(String missingKey) {
String summary = "size=" + new Integer(m_readerHashMap.size()).toString() + " elements[";
Enumeration e = m_readerHashMap.keys();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
if(key.equals(missingKey)) {
summary += "found_missing_key!";
}
summary += key + ",";
}
summary += "]";
return summary;
}

DEBUG Internal work queue does not contain sysobject 08001a908000016f, runtime error.
DEBUG because sysobject 08001a908000016f was not found, size=121 elements[09001a908000031b,09001a908000015f,08001a908000024c,08001a908000024e,08001a908000020b,08001a908000020d,08001a908000020f,09001a9080000330,09001a9080000254,08001a9080000185,08001a9080000265,09001a9080000336,09001a908000016a,09001a908000016c,09001a908000016e,09001a908000032c,08001a908000025f,09001a9080000187,09001a9080000189,08001a9080000278,09001a908000029e,08001a908000026a,09001a908000033f,08001a9080000281,09001a9080000151,09001a9080000153,09001a9080000155,09001a9080000157,09001a9080000159,08001a9080000246,08001a9080000248,09001a90800002a2,08001a908000023a,09001a908000014f,08001a908000023c,09001a9080000160,09001a9080000162,09001a9080000164,08001a9080000251,09001a9080000322,09001a9080000166,08001a9080000253,09001a9080000324,09001a9080000168,08001a9080000255,09001a908000027b,09001a908000027d,09001a908000015a,08001a908000028c,09001a908000015c,08001a908000028e,09001a908000015e,09001a908000031c,08001a908000024d,08001a908000016f,09001a908000031e,08001a908000024f,09001a9080000171,08001a9080000262,09001a9080000333,08001a9080000186,08001a9080000221,09001a9080000179,08001a9080000223,08001a9080000268,09001a9080000339,08001a9080000225,08001a9080000227,08001a9080000229,09001a908000016b,08001a908000017a,09001a908000016d,08001a908000025a,08001a908000025c,08001a908000021b,08001a908000021d,08001a908000021f,09001a9080000342,08001a9080000230,09001a9080000188,08001a9080000232,08001a9080000234,08001a9080000236,08001a9080000238,08001a908000026b,09001a908000033c,08001a908000022a,08001a908000022c,08001a908000022e,09001a9080000273,09001a9080000150,09001a9080000152,09001a9080000154,09001a9080000156,09001a9080000158,08001a9080000247,09001a9080000318,08001a9080000249,08001a9080000206,09001a908000026f,09001a908000014e,09001a9080000161,09001a9080000163,08001a9080000250,09001a9080000321,09001a9080000165,08001a9080000252,09001a9080000167,09001a9080000325,08001a9080000211,09001a9080000169,08001a9080000178,08001a9080000213,09001a9080000329,08001a9080000215,08001a9080000217,08001a9080000219,09001a908000027c,09001a908000027e,09001a908000015b,09001a908000015d,]

 

Posts:316
Registered: 4/7/04
Re: string equality in a HashMap  
Aug 4, 2004 8:31 PM (reply 1 of 10)



 
Question:
String a = new String(?08001a908000016f?);
String b = new String(?08001a908000016f?);

Will a.equals(b) always be true? This apparently
Yes, however you should do:
String a = "08001a908000016f";String b = "08001a908000016f";

Avoid using new String(). Strings are immutable

To find out if a key is in a map:
HashMap<String, String> m_readerHashMap = new HashMap<String, String>();...public String debugGetHashMapSummary(String missingKey) {   return m_readerHashMap.get(missingKey);}
 

Posts:316
Registered: 4/7/04
Re: string equality in a HashMap  
Aug 4, 2004 8:37 PM (reply 2 of 10)



 
You can use boolean HashMap.[url=http://java.sun.com/j2se/1.5.0/docs/api/java/util/HashMap.html#containsKey(java.lang.Object)]containsKey(Object key)[/url] to test if the map has a mapping for the specified key.
 

Posts:7
Registered: 10/11/98
Re: string equality in a HashMap  
Aug 4, 2004 8:41 PM (reply 3 of 10)



 
I realize that strings are immutablem but how does that explain your claim that
String a = "08001a908000016f";
is any different from
String a = new String("08001a908000016f");
?

Also using m_readerHashMap.get() will just return null. If I do a m_readerHashMap.contains(missingKey) it is not found - hence my original question.
 

Posts:7
Registered: 10/11/98
Re: string equality in a HashMap  
Aug 4, 2004 8:43 PM (reply 4 of 10)



 
You can use boolean HashMap.containsKey(Object key) to test if the map has a mapping for the specified key.

Yup, tried that, doesn't always work. That's why I posted a question.
 

Posts:5,965
Registered: 5/17/03
Re: string equality in a HashMap  
Aug 4, 2004 10:13 PM (reply 5 of 10)



 
The key String is missing because you didn't put it in the HashMap.

In mysterious case like this in 99.9% of the cases the programmer has made a mistake.

You can check this by tracing. Dump the whole data structure each time you make a change to it.
 

Posts:316
Registered: 4/7/04
Re: string equality in a HashMap  
Aug 5, 2004 7:34 AM (reply 6 of 10)



 
I realize that strings are immutablem but how does
that explain your claim that
String a = "08001a908000016f";
is any different from
String a = new String("08001a908000016f");

This is not "my claim", it is well documented.
// This explains the difference// == is for reference comparisonsclass StringReferenceComparisonTest {  public static void main(String[] args) {     System.out.println("abcdefghij" == "abcdefghij"); //true     System.out.println(new String("abcdefghij") == new String("abcdefghij")); //false     System.out.println(new String("a") == new String("a")); //false  }}


Also using m_readerHashMap.get() will just return
null. If I do a m_readerHashMap.contains(missingKey)
it is not found - hence my original question.

Do as UlrikaJ stated and print out the entire map. If the [url=http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/ConcurrentHashMap.html#containsKey(java.lang.Object)]containsKey()[/url] returns false, then according to the API, the key is not in the map. I notice you are not using [url=http://java.sun.com/j2se/1.5.0/docs/api/java/util/HashMap.html]java.util.HashMap[/url], therefore I can't tell if the problem is in [url=http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/ConcurrentHashMap.html]edu.oswego.cs.dl.util.concurrent.ConcurrentHashMap [/url] or in your code (although it is probably the your code if [url=http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/ConcurrentHashMap.html]edu.oswego.cs.dl.util.concurrent.ConcurrentHashMap [/url] has been well tested).
 

Posts:161
Registered: 8/7/97
Re: string equality in a HashMap  
Aug 5, 2004 8:08 AM (reply 7 of 10)



 
If you are using the concurrency library you have probably multiple threads that access these data . Are you sure that you fully understand what your program does and how the classs from the library work? Or does this really heappen with only one thread running?
 

Posts:7
Registered: 10/11/98
Re: string equality in a HashMap  
Aug 6, 2004 11:25 AM (reply 8 of 10)



 
So it turned out it wasn't a threading problem etc.

The hashcodes were different, so I looked at the bytes and turns out there were single quotes around the string, which didn't show up on log4j traces. The quoted strings come from a third party search engine my program integrates with, sometimes it puts quotes, sometimes not... hence the confusion.

So the lesson learned, is always look at the hashCode and byte values and don't trust log4j traces....

missing key looked like
key=08001a9080000250,
hashCode=1271569372,
bytes=394856484849975748564848484850534839

hashmap value was like
value=08001a9080000250,
hashCode=325568388,
bytes=48564848499757485648484848505348
 

Posts:37,103
Registered: 3/30/99
Re: string equality in a HashMap  
Aug 6, 2004 12:18 PM (reply 9 of 10)



 
I don't see why log4j is the problem. Sounds like it faithfully logged what was given to it,
including the surrounding quotes if they were present. Sounds more like the moral is
to make sure you're logging and viewing what you think you're logging and viewing.
 

Posts:7
Registered: 10/11/98
Re: string equality in a HashMap  
Aug 8, 2004 8:26 PM (reply 10 of 10)



 
jverd, the single quotes didn't show up in the log4j traces!

If one knew exactly what was going on in a program already - what would be the point of having log4j?
 
This topic has 10 replies on 1 page.