JDK-8048188 : (array) Arrays.equals(Object[], Object[]) should be smarter about calling Object.equals
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util
  • Affected Version: 8u5
  • Priority: P5
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_2003
  • CPU: x86
  • Submitted: 2014-06-21
  • Updated: 2018-01-11
  • Resolved: 2018-01-11
Related Reports
Duplicate :  
Description
A DESCRIPTION OF THE REQUEST :
In the implementation of java.util.Arrays.equals(Object[], Object[]), the loop calls Object.equals for every pair of non-null objects.

        for (int i=0; i<length; i++) {
            Object o1 = a[i];
            Object o2 = a2[i];
            if (!(o1==null ? o2==null : o1.equals(o2)))
                return false;
        }

The following (in the manner of java.util.Objects.equals) would be faster, because it would skip calling equals for all pairs that are simply ==.

        for (int i=0; i<length; i++) {
            Object o1 = a[i];
            Object o2 = a2[i];
            if (o1 != o2 && (o1 == null || !o1.equals(o2)))
                return false;
        }

JUSTIFICATION :
Widely used utility methods like this should have best-possible optimization because this can benefit many programs.



Comments
As of the change for JDK-8033148, the Arrays.equals() for comparing Object[] calls into Objects.equals(), so we now skip calling equals() for pairs that are ==. Closing as a duplicate.
11-01-2018

A micro-benchmark should be created to prove it is worth doing. Another concern is that such a change would break comparing of objects, which violate the convention of Object.equals() being reflexive.
26-06-2014