HashMap允许重复项,而StringBuilder不能正确反转。

huangapple go评论74阅读模式
英文:

Why does HashMap allow duplicates and StringBuilder does not reverse properly?

问题

HashMap不应允许重复项,而StringBuilder.reverse()方法不起作用的原因是什么?

String s = "abba";
static int myMethod(String s) {
        int counter = 0;
        Map<StringBuilder, StringBuilder> map = new HashMap<>();
        for (int j = 0; j <= s.length(); j++) {
            for (int i = j + 1; i <= s.length(); i++) {
                StringBuilder sb = new StringBuilder(s.substring(j, i));
                map.put(sb, sb.reverse());
            }
        }
        System.out.println("map " + map);
        return counter;
    }

输出:

map {bba=bba, ba=ba, b=b, a=a, a=a, ab=ab, abb=abb, abba=abba, b=b, bb=bb}

英文:

HashMap should not allow duplicates, while StringBuilder.reverse() does not work properly. Why?

String s = &quot;abba&quot;;
static int myMethod(String s) {
        int counter = 0;
        Map&lt;StringBuilder, StringBuilder&gt; map = new HashMap&lt;&gt;();
        for (int j = 0; j &lt;= s.length(); j++) {
            for (int i = j + 1; i &lt;= s.length(); i++) {
                StringBuilder sb = new StringBuilder(s.substring(j, i));
                map.put(sb, sb.reverse());
            }
        }
        System.out.println(&quot;map &quot; + map);
        return counter;
    }

Output:

map {bba=bba, ba=ba, b=b, a=a, a=a, ab=ab, abb=abb, abba=abba, b=b, bb=bb}

答案1

得分: 4

HashMap不允许重复。但是问题在于如何确定这些重复是如何确定的,这取决于你为什么认为它允许重复。

它使用hashCode()equals()来确定两个东西是否相同。StringBuilder都没有重写它们,因此使用来自Object的实现,这些实现基于对象的标识。因此,两个不同的StringBuilder实例 - 无论内部缓冲区的内容如何 - 都不相等。


reverse()是在原地反转StringBuilder。因此,sbsb.reverse()是同一个StringBuilder

显然,同一个实例只能按照一种顺序具有其字符。你在这里看到的是你将反转后的字符串同时存储为键和值。

英文:

HashMap doesn't allow duplicates. But it's a question of how those duplicates are determined as to why you might think it does.

It uses hashCode() and equals() to determine if two things are the same. StringBuilder overrides neither, and so uses the implementations from Object, which are based on identity. As such, two distinct instances of StringBuilder - irrespective of the contents of the internal buffer - are unequal.


reverse() is reversing the StringBuilder in place. As such, sb and sb.reverse() are the same StringBuilder.

Obviously, the same instance can only have its characters in a single order. What you are seeing here is that you are storing the reversed string as both the key and value.

答案2

得分: 2

不应该将 StringBuilder 存储在 HashMap 中,因为与 String 不同,它没有根据其内容实现 equals()hashCode() 方法(它从 Object 继承了这些方法)。两个不同的 StringBuilder 实例永远不会相等。为了使程序正常运行并防止重复,应该存储 String

String s = "abba";
int counter = 0;
Map<String, String> map = new HashMap<>();
for (int j = 0; j <= s.length(); j++) {
     for (int i = j + 1; i <= s.length(); i++) {
          StringBuilder sb = new StringBuilder(s.substring(j, i));
          StringBuilder reverse = sb.reverse();
          map.put(s.substring(j, i), reverse.toString());
     }
}
System.out.println("map " + map);

输出:

map {abb=bba, bb=bb, bba=abb, a=a, ab=ba, b=b, abba=abba, ba=ab}
英文:

You shouldn't store StringBuilder in the HashMap, because unlike String it does not implement equals() and hashCode() methods based on its content (it inherits them from Object). Two different instances of a StringBuilder can never be equal. In order for the program to function properly and prevent duplicates, store String instead:

String s = &quot;abba&quot;;
int counter = 0;
Map&lt;String, String&gt; map = new HashMap&lt;&gt;();
for (int j = 0; j &lt;= s.length(); j++) {
     for (int i = j + 1; i &lt;= s.length(); i++) {
          StringBuilder sb = new StringBuilder(s.substring(j, i));
          StringBuilder reverse = sb.reverse();
          map.put(s.substring(j, i), reverse.toString());
     }
}
System.out.println(&quot;map &quot; + map);

Output:

map {abb=bba, bb=bb, bba=abb, a=a, ab=ba, b=b, abba=abba, ba=ab}

huangapple
  • 本文由 发表于 2020年8月4日 06:06:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63237575.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定