Skip to content
Tutorials Center
My learning

Topics

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.

Java
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

Java fill and equals

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.

Java Deep comparison and hashing

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

Java
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 use Arrays.equals() (or deepEquals() for nested arrays) to compare content.
  • Arrays.equals() accepts null. Two null references are equal; null versus an empty array is not.
  • Floating point: equals() on double[] compares bit patterns via Double.doubleToLongBits, so NaN equals NaN, while 0.0 and -0.0 are different.
  • fill() with objects shares one instance. Every slot refers to the same object. To create distinct objects, loop and call new for each slot, or use Arrays.setAll().
  • Multi-dimensional arrays: Arrays.fill(grid, 0) does not compile for int[][]; fill each row instead: for (int[] row : grid) Arrays.fill(row, 0).
  • hashCode() uses the same formula as List.hashCode() (31 times the running result plus the element hash), so equal content gives an equal hash. Do not use an array itself as a HashMap key; wrap it in a List or 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.