Results 1 to 9 of 9
Thread: HashMap problems
- 05-04-2011, 08:55 PM #1
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:
PHP 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; } }Last edited by Toll; 05-04-2011 at 09:41 PM. Reason: Putting the code in PHP-tags instead of CODE-tags
- 05-04-2011, 09:08 PM #2
Oh, and here's the part where I try to get the values:
And the result (after removing the System.out.println in the hashCode-function in the FileSizeHashIdentifier class):PHP 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; } }
Java 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.
Last edited by Toll; 05-04-2011 at 09:42 PM. Reason: Changing CODE-tags to PHP-tags
- 05-06-2011, 03:32 PM #3
Anyone have a clue?
- 05-06-2011, 03:37 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-06-2011, 04:22 PM #5
Nope, no luck.
- 05-06-2011, 04:35 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
-
Sharp eye!
- 05-06-2011, 06:18 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 05-06-2011, 06:44 PM #9
Similar Threads
-
Hashmap to TXT and TXT to Hashmap
By elvinny in forum Advanced JavaReplies: 4Last Post: 02-16-2011, 11:12 PM -
How to create a new HashMap from a HashMap entries of other methods
By pandeyalok in forum Advanced JavaReplies: 7Last Post: 12-08-2009, 07:17 PM -
Problems with hashMap, has values in it but can't find them?
By mainy in forum New To JavaReplies: 5Last Post: 07-28-2009, 10:22 PM -
HashMap
By koolhoney in forum Advanced JavaReplies: 1Last Post: 03-30-2009, 08:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks