HashMap 的意外输出
代码:
public class Puzzle23{
void Puzzle23(){
map1.put(String1, "1");
map1.put(String2, "2");
}
private final NewMap map1 = new NewMap();
private static final String String1 = new String("J2eeSig");
private static final String String2 = new String("J2eeSig");
public static void main(final String args[]){
final Puzzle23 p22 = new Puzzle23();
final Map<String, String> map2 = new HashMap();
map2.put(String1, "1");
map2.put(String2, "2");
System.out.println(p22.map1.size() == map2.size() ? true : false);
p22.map1.remove(new String(String1));
map2.remove(new String(String2));
System.out.println(p22.map1.size() == map2.size() ? true : false);
}
class NewMap extends IdentityHashMap<String, String>{
public void put(final String... values){
super.put(values[0], values[1]);
}
public int size(){
return super.size() + 1 - 1 / 1 * 1;
}
}
}
实际结果:-
false
true
预期结果:-
true
true
为什么???
CODE:
public class Puzzle23{
void Puzzle23(){
map1.put(String1, "1");
map1.put(String2, "2");
}
private final NewMap map1 = new NewMap();
private static final String String1 = new String("J2eeSig");
private static final String String2 = new String("J2eeSig");
public static void main(final String args[]){
final Puzzle23 p22 = new Puzzle23();
final Map<String, String> map2 = new HashMap();
map2.put(String1, "1");
map2.put(String2, "2");
System.out.println(p22.map1.size() == map2.size() ? true : false);
p22.map1.remove(new String(String1));
map2.remove(new String(String2));
System.out.println(p22.map1.size() == map2.size() ? true : false);
}
class NewMap extends IdentityHashMap<String, String>{
public void put(final String... values){
super.put(values[0], values[1]);
}
public int size(){
return super.size() + 1 - 1 / 1 * 1;
}
}
}
Actual Result:-
false
true
Expected Result:-
true
true
Why???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为使用
NewMap
是IdentityHashMap
。检查文档中所说的地方编辑:
无论如何,我在你的代码中发现了一个错误。
void Puzzle23()
不是构造函数,它是一个方法。必须定义没有返回值的构造函数(例如Puzzle23()
)。所以你永远不会填充map1
。当您修复此问题时,您会发现由于IdentityHashMap
,您的输出为false
false
。当您将map1
切换到HashMap
时,输出将为true
true
正如您所期望的。无论如何,请检查IdentityHashMap
的文档。it's because of use
NewMap
isIdentityHashMap
. Check documentation where is saidEDIT:
Anyway I found a error in your code.
void Puzzle23()
is not constructor it's a method. Constructor have to be defined without return value (e.g.Puzzle23()
). So you never fillmap1
. When you fix this you realize that your output isfalse
false
because ofIdentityHashMap
. When you switchmap1
toHashMap
output will betrue
true
as you expected. Anyway check documentation ofIdentityHashMap
.IdentityHashMap
使用==
,而普通HashMap
使用.equals()
。请参阅文档。IdentityHashMap
uses==
where ordinaryHashMap
uses.equals()
. See documentation.p22.map1.remove(new String(String1));
不会删除任何内容,因为NewMap
是IdentityHashMap
的子类。更新
我错了
1) 这里的第一个错误是伪装成构造函数的
void
方法。这就是为什么p22.map1
始终为空。2) 第二个是
NewMap
,它是IdentityHashMap
。添加 2 个字符串后,其大小变为 2,因为虽然这些字符串相等,但它们并不相同 (==
)。3)
p22.map1.remove(new String(String1));
不会像我之前说的那样做任何事情。p22.map1.remove(new String(String1));
will not remove anything becauseNewMap
is a subclass ofIdentityHashMap
.Update
I was wrong
1) First error here is
void
method disguised as constructor. That's whyp22.map1
is always empty.2) Second one is a
NewMap
which isIdentityHashMap
. After 2 strings is added to it its size becomes 2 because while this strings are equal they are not identical (==
).3)
p22.map1.remove(new String(String1));
will not do anything as I said earlier.