I write a complex program in Java where I need to manage many large sparse matrix that contain integer values. Example:
int A[][][][];
int B[][][];
int C[][];
In order to saving memory, space, etc, I decided to store this data in TreeMap. I know that exist a lot of libraries that do it better but I would try to implement my personal solution. First of all I created a class Index that identifies the indeces of this array without knowing how many they are.
public class Index implements Comparable<Index> {
private final int[] index;
public Index(int ... index) {
this.index = index;
}
@Override
public int toCompare(Index i) {
...
}
}
Obviously I've used this class to define my three Maps in this way:
Map <Index, Integer> mapA = new TreeMap <> ();
Map <Index, Integer> mapB = new TreeMap <> ();
Map <Index, Integer> mapC = new ...
Now, I need to store the data from the matrix that I previous create to those Maps. The most easy way is use 4/3/2/.. loops, but I accept with pleasure others solutions.
for(int s = 0; s < s_max; s++) {
for(int c = 0; c < c_max; c++) {
for(int o = 0; o < o_max; o++) {
for(int d = 0; d < d_max; d++) {
mapA.put(new Index(s,c,o,d), A[s][c][o][d]);
}
}
}
}
The focus of the problem is when I need to retrieve the data, but let's me explain. The more usually operations between those maps are multiplications and additions considering them as matrix or arrays. I can't make 6(..) nested loops every times that I need to perform operations likes this below (I can't insert images because my reputation is still low):
http://postimg.org/image/7m8v0kiwn/
My questions are:
- How can I filter my values from keys?
- There is a way (maybe using lambda expression) to perform this type of operations?
- Any advices about this problem?
From: http://stackoverflow.com/questions/390181/sparse-matrices-arrays-in-java – Samuele Colombo Apr 20 '15 at 15:41