java - HashMap 中的字符串相等

标签 java hashmap equality

因此,我创建了一个 HashMap(资源是我自己创建的),其中每个键都是每个资源的文件路径。现在,假设我在 HashMap 中有一个文件路径为“mat\10wdim3.mat”的资源,尝试检索它将失败,因为两个哈希码不相等。

该键在映射中的哈希码是:-347056295 我用来尝试检索该资源的字符串:-2128683668

我在这两个字符串中都找不到隐藏字符。还有其他方法可以使哈希码不匹配吗?

编辑:

包括一些示例代码

在构造函数中初始化。创建并添加资源(文件路径已预先修剪)

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

获取所述资源

ResourceManager rManager = new ResourceManager();

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

Resource res = rManager.getResource("mat\10wdim3.mat");

最佳答案

问题是您没有将字母“\”转义为“\\”;

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

但是如果你转义字母“\”,你将得到以下输出:

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

所以,当你从HashMap中获取资源时,只需转义filePath字符串即可!

关于java - HashMap 中的字符串相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9817136/

相关文章:

java - 我们可以用 Java 模拟 Web 容器吗?

java - 有没有办法为 OpenNLP 获取 "original"文本数据?

java - 即使没有 "mapping"也使用 Java HashMap ?

java - 在 ORMLite 中持久化 HashMap

java - JAX-RS 使用 Map<String, Object> 作为 JSON 请求的一部分

javascript - 为什么使用相同的函数返回不同的对象来映射数组?

objective-c - 比较 isEqual 中的 nil 属性

typescript : Object Equality Comparison (Object Equals Object)

java - 如何在JFrame中排列,升序,降序,冒泡,输入区域,输出区域

java - 如何将枚举干净地链接到类中的静态信息?