-
HashMap problems
Hi!
In short, I'm developing a program to keep track of files. I also want to be able to search through the file list quickly, which is what I intend to use a HashMap for (so I can detect duplicates with ease), and since the server I synchronise with uses a combination of an MD4 hashcode and the file size, I want to do the same thing. For that reason I created a (very) simple class that contains just these two values, along with an equals-function and its own hashCode. However, when I put this class into the HashMap as the key, it cannot be found again! If I parse through the HashMap with an iterator, I can find an object that has the same hashCode and they equal eachother, but with HashMap.get(o) it returns null, and HashMap.contains(o) returns false. Not sure how much code you like to look at, but this is the class that I use to combine the two values:
Code:
class FileSizeHashIdentifier
{
long size;
String hash;
public FileSizeHashIdentifier(long size, String hash)
{
this.size=size;
this.hash=hash;
}
public boolean equals(FileSizeHashIdentifier f)
{
return f.size==size && f.hash.equals(hash);
}
public String toString()
{
return size+"\\"+hash;
}
public int hashCode()
{
long key1=Long.parseLong(hash.substring(0, 8), 16);
long key2=Long.parseLong(hash.substring(8, 16), 16);
long key3=Long.parseLong(hash.substring(16, 24), 16);
long key4=Long.parseLong(hash.substring(24, 32), 16);
long bigkey=key1+key2+key3+key4+size;
int hashcode=(int)(bigkey%Integer.MAX_VALUE);
System.out.println("Hashing: "+key1+"\\"+key2+"\\"+key3+"\\"+key4+"\\"+size+":"+bigkey+"("+hashcode+")");
return hashcode;
}
}
-
Oh, and here's the part where I try to get the values:
Code:
FileSizeHashIdentifier fshi=new FileSizeHashIdentifier(f.length(), checksum.getFormattedValue());
System.out.println(fshi);
HashMap<String, String> fileinfo=fileHashbuffer.get(fshi);
System.out.println("Gotten from get-function: "+fileinfo);
System.out.println("Does it exist in the HashMap? "+fileHashbuffer.containsKey(fshi));
Iterator<FileSizeHashIdentifier> iter=fileHashbuffer.keySet().iterator();
while (iter.hasNext())
{
FileSizeHashIdentifier toTest=iter.next();
System.out.println("Comparing "+fshi+" to "+toTest+": "+fshi.equals(toTest));
if (fshi.equals(toTest))
{
System.out.println("Got a match. Checking hash.");
System.out.println("Do the hashes match: "+(fshi.hashCode()==toTest.hashCode()));
System.out.println(fileHashbuffer.get(toTest));
System.out.println(fileHashbuffer.get(fshi));
System.out.println(toTest.hashCode());
System.out.println(fshi.hashCode());
fileinfo=fileHashbuffer.get(toTest);
break;
}
}
And the result (after removing the System.out.println in the hashCode-function in the FileSizeHashIdentifier class):
Code:
314105329\33be78bda111e7c1f2dcc8c79777ce6d
Gotten from get-function: null
Does it exist in the HashMap? false
Comparing 314105329\33be78bda111e7c1f2dcc8c79777ce6d to 200640714\acbe15d47be9c6
e6cb8a23712b38110f: false
Comparing 314105329\33be78bda111e7c1f2dcc8c79777ce6d to 200640714\acbe15d47be9c6
e6cb8a23712b38110f: false
Comparing 314105329\33be78bda111e7c1f2dcc8c79777ce6d to 314105329\33be78bda111e7
c1f2dcc8c79777ce6d: true
Got a match. Checking hash.
Do the hashes match: true
// Long chunk of filedata here
null
1910363559
1910363559
Found some filedata. Parsing.
-
-
Quote:
Originally Posted by
Toll
Anyone have a clue?
I don't see it; comment out the body of your hashCode() method and replace it by "return 42;" and see what happens. The equals( ... ) method seems ok to me but I'm probably blind ...
kind regards,
Jos
-
-
Quote:
Originally Posted by
Toll
Code:
public boolean equals(FileSizeHashIdentifier f)
{
return f.size==size && f.hash.equals(hash);
}
Got it: the signature of the equals( ... ) method is:
Code:
public boolean equals(Object that) { ... }
Your method isn't even been called.
kind regards,
Jos
-
-
Quote:
Originally Posted by
Fubarable
Sharp eye!
Mwah, I've made that mistake ever so often; it surprised me that I didn't see it after a first read ;-)
kind regards,
Jos
-
Ah, that did the trick. Thanks a bunch!