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

Equals does not work for Optional arrays.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Fix
    • Icon: P4 P4
    • None
    • 8u40
    • core-libs
    • x86_64
    • linux

      FULL PRODUCT VERSION :
      1.8.0_40-b25

      ADDITIONAL OS VERSION INFORMATION :
      Linux 3.13.0-45-generic #74-Ubuntu SMP x86_64 GNU/Linux
      Also tested on Windows 8.

      A DESCRIPTION OF THE PROBLEM :
      When wrapping two identical arrays in Optional (e.g. Optional<byte[]>) there is no method to test for equality. Not even an Arrays.equals() for Optionals. Ideally, calling .equals() on an Optional would call the appropriate method to compare contents.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create two identical arrays, wrap them in Optional, and compare them.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I expect to compare Optionals with identical contents to return true when calling equals.
      ACTUAL -
      Optionals with arrays are never equal.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      Optional<int[]> ints1 = Optional.of(new int[]{1, 2, 3});
      Optional<int[]> ints2 = Optional.of(new int[]{1, 2, 3});
      if(!ints1.equals(ints2)) {
        System.out.println("Should be equal.");
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      By extending Optional locally with my own version of equals that tests if the contents are an array and calling Arrays.equals() .

            psandoz Paul Sandoz
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: