Unrealomega
Unrealomega

Reputation: 107

String in HashMap equality

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");

Upvotes: 1

Views: 522

Answers (6)

codeisee
codeisee

Reputation: 1732

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!

Upvotes: 2

Noroi
Noroi

Reputation: 71

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.

Upvotes: 0

Jasper Sprengers
Jasper Sprengers

Reputation: 531

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.

Upvotes: 0

Esben Skov Pedersen
Esben Skov Pedersen

Reputation: 4517

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()

Upvotes: 0

Drew
Drew

Reputation: 426

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

Upvotes: 0

Brian Roach
Brian Roach

Reputation: 76898

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()

Upvotes: 0

Related Questions