I am looking for a sparse matrix library in Java that can do multiplications on sparse integer matrices, where the matrices represent the adjacency relations of a graph. The requirement is roughly the following: the library should be able to load and multiply a few matrices of 10M×10M elements, containing approx. 5M non-zero elements each, when running on a commodity machine (~16 GBs of RAM). The Eigen library for C++ satisfies this requirement. However, I couldn't find a good alternative for Java.

I looked at the following libraries:

I have also found a comprehensive survey at https://java-matrix.org/, created by the author of UJMP, which shows the state of the art in ~2015 and highlights that very few libraries support sparse matrices.

See also the GitHub issue on the performance of sparse matrix multiplication in MTJ.

A related question for a C/C++ library from 2010: Looking for a C/C++ interface for efficient computation of huge sparse matrix in Linux

Update (late 2019): I've now researched this area for over a year and identified a few requirements that sparse matrix libraries need to meet in order to express graph algorithms and perform them efficiently:

  1. support the definition of semirings other than the conventional plus-times arithmetic one (e.g. lor-land, min-plus)
  2. support masking operations (only perform a given operation for elements selected by a mask)
  3. allow multi-threaded execution

As of now, I could not find a Java library that satisfies any of the requirements above. Therefore, I have switched to C and use SuiteSparse:GraphBLAS.

Update (Sep 2020): EJML now satisfies requirement #1, see the pull request introducing support for semirings.

Update (Feb 2021): EJML v0.40 supports all requirements, offering some degree of concurrency and masks.


1 Answer 1


There are two libraries I can recommend. Both support sparse matrices, including matrix-matrix multiplication, matrix-matrix element-wise multiplication and matrix-vector multiplication. Both are open-source, actively maintained, and available from the Maven Central.

  1. EJML (Efficient Java Matrix Library) is Apache-licensed (ASLv2) - [source code], [Maven]. Recently (Sep 2020) it has been extended with support for semirings.

  2. ojAlgo ("oj! Algorithms") is MIT-licensed - [source code], [Maven].

For the sparse workloads I tested, EJML had far superior performance, so it's worth trying that one first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.