英文:
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 = "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;
}
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
。因此,sb
和sb.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 = "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);
Output:
map {abb=bba, bb=bb, bba=abb, a=a, ab=ba, b=b, abba=abba, ba=ab}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论