为什么可以使用字符串作为 HashMap 中的键?

发布于 2025-01-08 15:22:41 字数 477 浏览 2 评论 0原文

如果两个相同的 String 实际上并不相同,那么为什么我可以使用字符串作为 HashMap 中的键而不使用相同的 String目的?

String s1 = "Test";
String s2 = "Test";

System.out.println(s1 == s2); // should be false
System.out.println(s1.equals(s2)); // should be true

HashMap<String, String> map = new HashMap();
map.put(s1, "foo");
System.out.println(map.get(s2)); // should be "foo"--but why?

HashMap 对于 String 对象是否有一些特殊行为?如果不是,为什么可以使用两个“不同”的字符串来从哈希中放入和获取值?

If two Strings that are the same are not actually identical, then why can I use strings as keys in a HashMap without using the same String object?

String s1 = "Test";
String s2 = "Test";

System.out.println(s1 == s2); // should be false
System.out.println(s1.equals(s2)); // should be true

HashMap<String, String> map = new HashMap();
map.put(s1, "foo");
System.out.println(map.get(s2)); // should be "foo"--but why?

Does HashMap have some special behavior for String objects? If not, why can two "different" strings be used to put and to get values from a hash?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

停滞 2025-01-15 15:22:41

HashMap 通过调用 equals()hashCode() 来比较对象。
String 重写这些方法以按值进行比较。

HashMap compares objects by calling equals() and hashCode().
String overrides these methods to compare by value.

国粹 2025-01-15 15:22:41

一般来说,您可以使用 String 对象,因为 HashMap 使用 equals() 而不是 == 来测试键相等性。

In general, you can use String objects because HashMap uses equals() and not == to test for key equality.

夜未央樱花落 2025-01-15 15:22:41

如果两个相同的字符串实际上并不相等

但它们是相等的。它们在 equals() 方法下相等,这是在 Map 接口中指定的相等测试技术。

System.out.println(s1 == s2); // should be false

但这并不假!由于编译器的常量池,两者都引用相同的字符串。

If two Strings that are the same are not actually equal

But they are. They are equal under the equals() method, and that is the technique specified for equality testing in the Map interface.

System.out.println(s1 == s2); // should be false

But it isn't false! Both refer to the same string because of constant pooling by the compiler.

﹏半生如梦愿梦如真 2025-01-15 15:22:41

HashMap内部比较键时,它使用equals()方法,而不是==。因此,对象相等对于键匹配来说是很好的,如果覆盖 equals() 则不需要引用相等(如 java.lang.String 的情况)。

When the HashMap compares the key internally, it uses the equals() method, not ==. So object equality is fine for a key match, reference equality is not required if equals() is overridden (as in the case of java.lang.String.)

陈独秀 2025-01-15 15:22:41

System.out.println(s1 == s2); // 应该为假

不应该。 Java编译器可能会优化并将两个字符串指向同一位置。

更新

public class Test {

    public static void main(String... args) {
        String s1 = "abc";
        String s2 = "abc";

        System.out.println(s1 == s2);
    }

}

输出

javac Test.java
java Test
> true

System.out.println(s1 == s2); // should be false

It should not. Java compiler may optimize and point two strings to the same location.

Update

public class Test {

    public static void main(String... args) {
        String s1 = "abc";
        String s2 = "abc";

        System.out.println(s1 == s2);
    }

}

Output

javac Test.java
java Test
> true
柏林苍穹下 2025-01-15 15:22:41

我们真的应该首先回到基础。

计算机科学基础:

== 比较内存地址(引用)

.equals 比较存储在内存地址

java basic 中的值。整个jvm中只有一份字符串对象的副本

we should really go back to the basic first.

computer science fundamental:

== compares memory address(reference)

.equals compares value stored in the memory address

java basic. there is only one copy of string object across entire jvm

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文