Java Reference ยท Collections
Java Arrays.fill(), equals() and hashCode()
Reference for java.util.Arrays.fill(), equals(), deepEquals(), hashCode() and mismatch(): compare arrays by content and set many elements at once.
Arrays in Java are objects, but they do not override equals(), hashCode() or toString(). Calling a.equals(b) on two arrays checks identity, not content. The java.util.Arrays class fills the gap with static helpers: fill() writes one value into many slots, equals() and deepEquals() compare content, and hashCode() and deepHashCode() compute a content-based hash.
These methods exist in overloads for every primitive array type and for Object[]. The Object[] versions of equals() and hashCode() are shallow: they call equals() on each element, so nested arrays are compared by identity. Use the deep variants for arrays of arrays.
int[] data = new int[5]; Arrays.fill(data, 7); // [7, 7, 7, 7, 7] boolean same = Arrays.equals(data, new int[]{7, 7, 7, 7, 7}); // true
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
static void fill(int[] a, int val) static void fill(int[] a, int from, int to, int val) static boolean equals(int[] a, int[] b) static boolean equals(int[] a, int aFrom, int aTo, int[] b, int bFrom, int bTo) static boolean deepEquals(Object[] a, Object[] b) static int hashCode(int[] a) static int deepHashCode(Object[] a) static int mismatch(int[] a, int[] b)
Overloads exist for byte, short, char, int, long, float, double, boolean and Object. The range and mismatch forms were added in Java 9.
| Name | Description |
|---|---|
a, b |
The arrays to fill or compare. |
from, to |
Start index (inclusive) and end index (exclusive). |
val |
The value written into every slot of the range. |
Returns: fill() returns nothing. equals() and deepEquals() return boolean. hashCode() returns an int. mismatch() returns the index of the first difference, or -1 if the arrays (or ranges) are identical; if one is a prefix of the other it returns the length of the shorter one.
Throws: ArrayIndexOutOfBoundsException if a range is outside the array, IllegalArgumentException if from > to, and NullPointerException from fill() or mismatch() when an array is null.
Usage notes
array.equals()is identity. Always useArrays.equals()(ordeepEquals()for nested arrays) to compare content.Arrays.equals()acceptsnull. Twonullreferences are equal;nullversus an empty array is not.- Floating point:
equals()ondouble[]compares bit patterns viaDouble.doubleToLongBits, soNaNequalsNaN, while0.0and-0.0are different. fill()with objects shares one instance. Every slot refers to the same object. To create distinct objects, loop and callnewfor each slot, or useArrays.setAll().- Multi-dimensional arrays:
Arrays.fill(grid, 0)does not compile forint[][]; fill each row instead:for (int[] row : grid) Arrays.fill(row, 0). hashCode()uses the same formula asList.hashCode()(31 times the running result plus the element hash), so equal content gives an equal hash. Do not use an array itself as aHashMapkey; wrap it in aListor a record, or the map will use identity.- Complexity: all of these are
O(n)in the range length. - Newly allocated arrays are already zero-filled (
0,false,null);fill()is only needed for other values.