Duplicate value in HashMap

I have big problems, I created a hash map and inserted two values ​​with the same key, using StringBuilder as the key to the map. Now, when trying to get data using the StringBuilder object, it works fine, but otherwise it will not be able to return any value. I cited three cases in the code below,

`

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
}

} `

+4
source share
6 answers

in the method receiveMap

System.out.println("Expected value a, not coming  => " + map.get(sb)); // why null ?

Since you are creating a new StringBuildersb in a method that has different hashcode compare with StringBuilder sb PSVM .

StringBuilder does not 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 , [sb, sb], StringBuilder equals hashcode.

+2

, StringBuilder (, - ). , 2 StringBuilder ( ) , , .

+1

String, StringBuilder equals hashCode, Object. sb.equals(sb1) , .

String StringBuilder .

+1

StringBuilder, .

, .

StringBuilder hashCode() equals(), HashMap .

System.out.println( " a, not coming = > " + map.get( "" ));//

StringBuilder object .

> 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

sb1 sb, , , .

Map<String, String> map = new HashMap<String, String>();

.toString(), , String hashCode() equals().

0

, Map, null. , , .

0
source

Using StringBuilder will never work, as one instance is NOT equal to another instance of StringBuilder

Correct your code for something like this

Map<String, String> map = new HashMap<String, String>();
        map.put(sb.toString(), "a");
        map.put(sb1.toString(), "b");

but since this is a map view, if sb.toString () and sb1.toString () are equal, as in your example, the value will be overridden

Thus, the result with corrections to your code

----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
0
source

Source: https://habr.com/ru/post/1615019/


All Articles