Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8140503

Improve javafx.util.Pair hash code computation

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 9
    • 8u66
    • javafx

      FULL PRODUCT VERSION :


      ADDITIONAL OS VERSION INFORMATION :
      64-bit Ubuntu 14.04

      A DESCRIPTION OF THE PROBLEM :
      The current implementation of the "hashCode" function in the convenience class "Pair" is as follows.

          public int hashCode() {
              // name's hashCode is multiplied by an arbitrary prime number (13)
              // in order to make sure there is a difference in the hashCode between
              // these two parameters:
              // name: a value: aa
              // name: aa value: a
              return key.hashCode() * 13 + (value == null ? 0 : value.hashCode());
          }

      However, this is an incorrect way to compute a hash code of two values. For example, here is a trivial collision of two hash codes which should be different.

          System.out.println(
              new Pair<Integer,Integer>(1,0).hashCode()
                  ==
              new Pair<Integer,Integer>(0,13).hashCode()
          ); // => true

      This can lead to hard-to-find bugs anywhere that instances of Pair are used in a data structure like a HashSet or HashTable.


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javafx.util.Pair;
      public class Solution {
          public static void main(String args[] ) throws Exception {
              System.out.println(
                  new Pair<Integer,Integer>(1,0).hashCode()
                      ==
                  new Pair<Integer,Integer>(0,13).hashCode());
          }
      }
      ---------- END SOURCE ----------

            vadim Vadim Pakhnushev
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: