I am currently working on the creation of a tab-delimited table for Excel from an ArrayList<String>
matrix in Java.
The matrix is a List<List<String>
and currently I am adding "\t"
to every single String in the matrix and "\n"
to the last elements of each row. I use for
loops to do so.
For a 1500 x 3000 matrix this process takes an insane amount of time (120s).
What is a better way too approach this problem in order to cut down on time?
Not sure which way your matrix is but could be built something like this I suppose. Dont see any obvious overhead here.
private final List<List<String>> matrix;
private final String SEPARATOR = "\t";
private final String END_OF_LINE = "\n";
public TSVFormatter(List<List<String>> matrix) {
this.matrix = matrix;
}
public String doParse() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < matrix.size(); i++) {
for (int o = 0; o < matrix.get(i).size(); o++) {
sb.append(matrix.get(i).get(o));
if (o <( matrix.get(i).size()-1))
sb.append(SEPARATOR);
else
sb.append(END_OF_LINE);
}
}
return sb.toString();
}