ClassCastException in `ConcurrentHashMap#equals`

XMLWordPrintable

    • Type: Bug
    • Resolution: Unresolved
    • Priority: P4
    • tbd
    • Affects Version/s: 21.0.9
    • Component/s: core-libs

      A DESCRIPTION OF THE PROBLEM :
      Assume we compare two maps by `equals`, where the first one is `ConcurrentHashMap`. It is important that these maps have different types of keys.
      `ConcurrentHashMap` calls `get` on the second map during comparison, but it is not prepared to `ClassCastException` that can be rightfully thrown from `Map#get`.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Just run the snippet.

      ---------- BEGIN SOURCE ----------
      ```
      public class Main {
          public static void main(String[] args) {
              var map = new ConcurrentHashMap<String, String>();
              map.put("key", "value");
              var map2 = new MyMap<String>();
              map2.put(1, "value");
              System.out.println(map.equals(map2));
          }

          static class MyMap<V> extends ConcurrentHashMap<Integer,V>{
              @Override
              public V get(Object key) {
                  Integer newKey = (Integer)key;
                  return super.get(newKey);
              }
          }
      }
      ```
      ---------- END SOURCE ----------

      FREQUENCY :
      ALWAYS

            Assignee:
            Viktor Klang
            Reporter:
            Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated: