1

So, I've created a HashMap(Resource being my own creation), which each key is the filepath for each resource. Now, let's say I have a Resource with a filepath "mat\10wdim3.mat" in the HashMap, trying to retrieve it will fail because both hashcodes are not equal.

The hashcode in the Map for this key is: -347056295 The string I'm using to try and retrieve that resource: -2128683668

There is no hidden characters I could find in either strings. Is there any other way that the hashcodes wouldn't match?

EDIT:

Including some sample code

Initializing in the constructor. Creating and adding the resources(The filepath is trimmed beforehand)

private Map<String, Resource> m_Files;

public ResourceManager() {
    m_Files = new HashMap<String, Resource>();
}

public Resource createResource(String filepath, byte[] contents) {
    if (filepath != null && contents != null && contents.length > 0) {          
        String extension;
        int lastIndex = filepath.lastIndexOf(".");
        Resource res = null;

        if (lastIndex == -1)
            return null;

        extension = filepath.substring(lastIndex).toLowerCase();

        // TODO: Optimize this by putting the most common at the top
        try {
            if (extension.equals(".pup"))
                res = new Puppet(contents);
            else if (extension.equals(".bm"))
                res = new Bitmap(contents);
            else if (extension.equals(".snd"))
                res = new SoundFile(contents);
            else if (extension.equals(".cmp"))
                res = new ColorMap(contents);
            else if (extension.equals(".mat"))
                res = new Mat(contents);
            else if (extension.equals(".sft"))
                res = new Font(contents);
            /*else if (extension.equals(".ai") // Normal AI
                    || extension.equals(".ai0") // Easy AI
                    || extension.equals(".ai2")) // Hard AI
                res = new AIFile(contents);*/

            if (res != null) {
                addResource(filepath, res);

                return res;
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
    }

    return null;
}

public void addResource(String filepath, Resource res) {
    if (filepath != null && res != null && m_Files.put(filepath, res) != null)
        System.out.println("[!] Replacing " + filepath);
}

public Resource getResource(String filepath) {
    return m_Files.get(filepath);
}

To get said resource

ResourceManager rManager = new ResourceManager();

rManager.loadGOB("C:/Documents and Settings/Unrealomega/Desktop/JKDF2/GOB/Resource/Res2.gob");

Resource res = rManager.getResource("mat\10wdim3.mat");
3
  • Key=filepath, value=resource object ? What is the deal with HashCodes? Can you provide some sample code? Mar 22, 2012 at 5:58
  • That shouldn't happen, not really, no. Actual code?
    – Affe
    Mar 22, 2012 at 5:59
  • It'd be kind of difficult to give you guys the entire code itself, but I can at least edit my post with how I'm adding to the HashMap. Mar 22, 2012 at 6:06

6 Answers 6

2

The problem is that you did not escape letter '\' to '\\';

"mat\10wdim3.mat".hashCode(); //-2128683668

but if you escape letter '\',you will get the follow ouput:

"mat\\10wdim3.mat".hashCode(); // -347056295

So,just escape the filePath String ,when you fetch the resource from HashMap!

1
  • Excellent. I haven't realized you had to escape \\, only //. Thanks! Mar 22, 2012 at 6:13
0

Because they're not the same object and you didn't override hashCode() and equals()

http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html#hashCode()

0

If you're comparing the string "mat\10wdim3.mat" with an object that represents the filepath "mat\10wdim3.mat" then you won't get a match because String and the object likely have different hashCode methods.

If you're definitely using strings or two other objects that have the same hashCode method then there should be no way that they are not equal

0

No there are no other way for hashing to be different provided the strings have the same content. Verify that your strings are indeed equal using

mystring.toCharArray()
0

Are you absolutely certain that the hashmap key and the String you're using are equal, i.e. string1.equals(string2) == true? If so, they must yield the same hashcode.

0

A String's hash value depends on the length of the String. Hence, we need to have the same String with the same length inorder to retrieve the value. There is no need to implement hashCode() and equals() as the key which is an Object of the String class already has implemented these two methods. Only thing you need to make sure is that the key is exactly the same key that you had used to put in the resource.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.