JDK-6792525 : equals doesn't works in Hashtable and other collections for array element(s)
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util:collections
  • Affected Version: 7
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2009-01-12
  • Updated: 2021-03-03
  • Resolved: 2010-07-28
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :


A DESCRIPTION OF THE PROBLEM :
If in one collection for example Hashtable we add some array value as element, then the equals method of that collections doesn't works. This is because in the implementation for arrays are not used special equals methods from java.util.Arrays.


REPRODUCIBILITY :
This bug can be reproduced always.

CUSTOMER SUBMITTED WORKAROUND :
Create you own equals method where before value.equals(t.get(key)) is used the following code:

        if(source instanceof boolean[])
            return Arrays.equals((boolean[])source, (boolean[])target);
        else if (source instanceof byte[])
            return Arrays.equals((byte[])source, (byte[])target);
        else if (source instanceof char[])
            return Arrays.equals((char[])source, (char[])target);
        else if (source instanceof double[])
            return Arrays.equals((double[])source, (double[])target);
        else if (source instanceof float[])
            return Arrays.equals((float[])source, (float[])target);
        else if (source instanceof int[])
            return Arrays.equals((int[])source, (int[])target);
        else if (source instanceof long[])
            return Arrays.equals((long[])source, (long[])target);
        else if (source instanceof short[])
            return Arrays.equals((short[])source, (short[])target);
        else if (source instanceof Object[])
            return Arrays.equals((Object[])source, (Object[])target);

Comments
WORK AROUND Use List as the element type instead of an array object and the list can be created from an array using the java.util.Arrays.asList() method.
28-07-2010

EVALUATION The specification for the Set.equals(Object) method is as follows: Compares the specified object with this set for equality. Returns true if the specified object is also a set, the two sets have the same size, and every member of the specified set is contained in this set (or equivalently, every member of this set is contained in the specified set). This definition ensures that the equals method works properly across different implementations of the set interface. A member of a set is contained in another set, as specified in the Set.contains(Object) method, Returns true if this collection contains the specified element. More formally, returns true if and only if this collection contains at least one element e such that (o==null ? e==null : o.equals(e)). If a member "o" of a set is an array (e.g. boolean[]) which is an object in the JVM, Object.equals() is invoked to check for equality. So this is not a bug. While this is a reasonable request, 4654312 will be a more proper way addressing it. A workaround is to use List as the element type rather than array object.
28-07-2010