This class offers expected time complexity O(1) (i.e. constant time) for the basic operations
get, getQuick, set, setQuick and size
assuming the hash function disperses the elements properly among the buckets.
Otherwise, pathological cases, although highly improbable, can occur, degrading performance to O(N) in the worst case.
As such this sparse class is expected to have no worse time complexity than its dense counterpart DenseObjectMatrix2D.
However, constant factors are considerably larger.

Cells are internally addressed in row-major.
Performance sensitive applications can exploit this fact.
Setting values in a loop row-by-row is quicker than column-by-column, because fewer hash collisions occur.
Thus

trimToSize()
Releases any superfluous memory created by explicitly putting zero values into cells formerly having non-zero values;
An application can use this operation to minimize the
storage of the receiver.

cardinality

ensureCapacity

public void ensureCapacity(int minCapacity)

Ensures that the receiver can hold at least the specified number of non-zero cells without needing to allocate new internal memory.
If necessary, allocates new internal memory and increases the capacity of the receiver.

This method never need be called; it is for performance tuning only.
Calling this method before tt>set()ing a large number of non-zero values boosts performance,
because the receiver will grow only once instead of potentially many times and hash collisions get less probable.

getQuick

Provided with invalid parameters this method may return invalid objects without throwing any exception.
You should only use this method when you are absolutely sure that the coordinate is within bounds.
Precondition (unchecked): 0 <= column < columns() && 0 <= row < rows().

like

Construct and returns a new empty matrix of the same dynamic type as the receiver, having the specified number of rows and columns.
For example, if the receiver is an instance of type DenseObjectMatrix2D the new matrix must also be of type DenseObjectMatrix2D,
if the receiver is an instance of type SparseObjectMatrix2D the new matrix must also be of type SparseObjectMatrix2D, etc.
In general, the new matrix should have internal parametrization as similar as possible.

like1D

Construct and returns a new 1-d matrix of the corresponding dynamic type, entirelly independent of the receiver.
For example, if the receiver is an instance of type DenseObjectMatrix2D the new matrix must be of type DenseObjectMatrix1D,
if the receiver is an instance of type SparseObjectMatrix2D the new matrix must be of type SparseObjectMatrix1D, etc.

setQuick

Sets the matrix cell at coordinate [row,column] to the specified value.

Provided with invalid parameters this method may access illegal indexes without throwing any exception.
You should only use this method when you are absolutely sure that the coordinate is within bounds.
Precondition (unchecked): 0 <= column < columns() && 0 <= row < rows().

trimToSize

Releases any superfluous memory created by explicitly putting zero values into cells formerly having non-zero values;
An application can use this operation to minimize the
storage of the receiver.

Background:

Cells that

are never set to non-zero values do not use any memory.

switch from zero to non-zero state do use memory.

switch back from non-zero to zero state also do use memory. However, their memory can be reclaimed by calling trimToSize().

A sequence like set(r,c,5); set(r,c,0);
sets a cell to non-zero state and later back to zero state.
Such as sequence generates obsolete memory that is automatically reclaimed from time to time or can manually be reclaimed by calling trimToSize().
Putting zeros into cells already containing zeros does not generate obsolete memory since no memory was allocated to them in the first place.