0

I want to create a 100k x 100k matrix in java. The code is going to be execute in an online editor (Hack Reactor), so I can't increase heap size. I found somewhere that I can use SparseMatrix, but in this online editor I can't import the jars of SparseVector. So can you help to solve the problem? Should I use multi threading?

ana123
  • 1
  • 3

1 Answers1

0

You can use Map as sparse matrix.

Just create class Pair and use it as key.

class Pair {
     final int x;
     final int y;
     Pair(int x, int y) {
         this.x=x;
         this.y=y;
     }

     public boolean equals(Object o) {
          Pair other = (Pair)o;
          return x==other.x&&y==other.y;
     }
     public int hashCode(){ return x ^ y; }
}

then declare

Map<Pair, Integer> matrix = new HashMap<>();

to set value use

matrix.put(new Pair(x, y), value);

to get value use

int value = matrix.get(new Pair(x, y));
talex
  • 17,973
  • 3
  • 29
  • 66