问题
I am in big trouble, created a hashMap and inserted two values with same key using StringBuilder as a key of map. Now, while trying to retrieve the data using StringBuilder object is working fine, but in other case its fails to return any value. I have listed three cases in the below given code,
`
class MainClass {
public static void main(String[] args) {
MainClass m = new MainClass();
StringBuilder sb = new StringBuilder("sb");
StringBuilder sb1 = new StringBuilder("sb");
Map<StringBuilder, String> map = new HashMap<StringBuilder, String>();
map.put(sb, "a");
map.put(sb1, "b");
System.out.println("----Inside Main method---- mapValue"+map);
System.out.println("Expected value a, coming also => " + map.get(sb)); //a
System.out.println("Expected value b, coming also => " + map.get(sb1)); //b
System.out.println("Expected value a, not coming => " + map.get("sb")); // why null ?
m.receiveMap(map, sb, sb1);
}
public void receiveMap(Map<StringBuilder, String> map, StringBuilder refSb,StringBuilder refSb1) {
StringBuilder sb = new StringBuilder("sb");
StringBuilder sb1 = new StringBuilder("sb");
System.out.println("----Inside receiveMap method mapValue"+map);
System.out.println("Expected value a, not coming => " + map.get(sb)); // why null ?
System.out.println("Expected value b, not coming => " + map.get(sb1)); // why null ?
System.out.println("Expected value a, coming also => " + map.get(refSb)); // o/p - a
System.out.println("Expected value b, coming also => " + map.get(refSb1)); // o/p -b
}
} `
回答1:
in Method receiveMap
System.out.println("Expected value a, not coming => " + map.get(sb)); // why null ?
Because you are creating new StringBuilder
sb in the method which having different
hashcode compare to StringBuilder sb PSVM.
StringBuilder doesn't override equals and hashCode
StringBuilder sb = new StringBuilder("sb");
StringBuilder sb1 = new StringBuilder("sb");
Set s = new HashSet();
s.add(sb);
s.add(sb1);
System.out.println(s);
Set does not allow duplicate but still it prints [sb, sb]
as StringBuilder does not override equals and hashcode method.
回答2:
Simple, one StringBuilder
instance is NOT equal to another (hence they will get different hashcodes, and are handled as separate entities) . Just because 2 StringBuilder
(or any Objects) have same value, that doesn't mean that they are the same.
回答3:
Unlike String
, StringBuilder
doesn't override equals
and hashCode
, and therefore is uses the default implementation of Object
class. Therefore sb.equals(sb1)
is false, even though they contain the same characters.
I suggest you use a String
instead of StringBuilder
as the key to your Map.
回答4:
You are passing objects which are not in Map so it returns null.Every time you pass correct object,you are getting value.
回答5:
Using StringBuilder it will never work as one instance is NOT equal to another StringBuilder instance
Correct your code to something like this
Map<String, String> map = new HashMap<String, String>();
map.put(sb.toString(), "a");
map.put(sb1.toString(), "b");
but as this is a map representation if the sb.toString() and sb1.toString() are equal like in your example the value will be overridden
So the output with the corrections to your code is
----Inside Main method---- mapValue{sb=b}
Expected value a, coming also => b
Expected value b, coming also => b
Expected value a, not coming => b
----Inside receiveMap method mapValue{sb=b}
Expected value a, not coming => b
Expected value b, not coming => b
Expected value a, coming also => b
Expected value b, coming also => b
回答6:
You have created different instances of StringBuilder
, which are not same.
You yourself have tested out almost all scenarios, confusion should have been clear.
StringBuilder
does not overrride hashCode()
and equals()
method, which HashMap
requires to work properly.
System.out.println("Expected value a, not coming => " + map.get("sb")); // why
new StringBuilder object
has been created and passed.
> StringBuilder sb = new StringBuilder("sb");
> StringBuilder sb1 = new StringBuilder("sb");
> System.out.println("----Inside receiveMap method mapValue" + map);
> System.out.println("Expected value a, not coming => " + map.get(sb)); // why
> System.out.println("Expected value b, not coming => " + map.get(sb1)); // why
here also new objects sb1
and sb
have been created which are not equal to previously created, though content is same, but references are different.
Also try changing Map to
Map<String, String> map = new HashMap<String, String>();
and use
.toString()
method, you will observe different behaviour, becauseString
class overrideshashCode()
andequals()
method.
来源:https://stackoverflow.com/questions/33606308/duplicate-value-in-hashmap