Results 1 to 9 of 9
  1. #1
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default 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 10:41 PM. Reason: Putting the code in PHP-tags instead of CODE-tags

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default

    Oh, and here's the part where I try to get the values:
    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;
              }
            }
    And the result (after removing the System.out.println in the hashCode-function in the FileSizeHashIdentifier class):
    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 10:42 PM. Reason: Changing CODE-tags to PHP-tags

  3. #3
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default

    Anyone have a clue?

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,787
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Toll View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default

    Nope, no luck.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,787
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Toll View Post
    Java Code:
    public boolean equals(FileSizeHashIdentifier f)
      {
        return f.size==size && f.hash.equals(hash);
      }
    Got it: the signature of the equals( ... ) method is:

    Java Code:
    public boolean equals(Object that) { ... }
    Your method isn't even been called.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Sharp eye!

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,787
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Fubarable View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

Similar Threads

  1. Hashmap to TXT and TXT to Hashmap
    By elvinny in forum Advanced Java
    Replies: 4
    Last Post: 02-17-2011, 12:12 AM
  2. Replies: 7
    Last Post: 12-08-2009, 08:17 PM
  3. Replies: 5
    Last Post: 07-28-2009, 11:22 PM
  4. HashMap
    By koolhoney in forum Advanced Java
    Replies: 1
    Last Post: 03-30-2009, 09:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •