Why do arrays in Java not override equals()?

I was working with a HashSet the other day, which has this written in the spec: [add()] adds the specified element e to this set if this set contains no element e2 such that (e==null ? e2==null : e.equals(e2)) I was using char[] in the HashSet until I realized that, based on this contract, it was no better than an ArrayList! Since it's using the non-overridden .equals(), my arrays will only be checked for reference equality, which is not particularly useful. I know that Arrays.equals() exists, but that doesn't help when one is using collections such as HashSet. So my question is, why would Java arrays not override equals?

Apr 30, 2025 - 10:21
 0
Why do arrays in Java not override equals()?

I was working with a HashSet the other day, which has this written in the spec:

[add()] adds the specified element e to this set if this set contains no element e2 such that (e==null ? e2==null : e.equals(e2))

I was using char[] in the HashSet until I realized that, based on this contract, it was no better than an ArrayList! Since it's using the non-overridden .equals(), my arrays will only be checked for reference equality, which is not particularly useful. I know that Arrays.equals() exists, but that doesn't help when one is using collections such as HashSet.

So my question is, why would Java arrays not override equals?