-1

I have an excel sheet, lets say :

enter image description here

Note that every row and column has its own name K , L ... and A,B.... Note also that every cell has the form (int , int ,int , int) that as far as i know is well represented with an arraylist. What is the best way to deal with that; Define a 2D array of arraylists; Or an arraylist of arraylists; Or using hash maps; I find it simplier to use a 2D array of arraylists in order to run loops but then i have to initialize every 2D array element as an arraylist ( or not;). Note that it is a big excel sheet ( over 2500 cells). Thanks in advance.

johnnal
  • 47
  • 1
  • 8

1 Answers1

0

If you need fast random access, you can use HashMap with cell keys.

public class CellKey {
    final String col;
    final String row;

    private CellKey () {} // no instatiation outside

    public CellKey at(String row, String column) {
        this.col = column;
        this.row = row;
    } 

    public getColumn() {
        return col;
    }

    public getRow() {
        return row;
    }

    public int hashCode() {
        return Objects.hash(col, row);
    }

    public boolean equals(CellKey other) {
        return this.col.equals(other.col) && this.row.equals(other.row);
    }
}

Now use it in collection representing cell values:

Map<CellKey, List<Integer>> cells = new HashMap<>();

// get value
cells.get(CellKey.at(row, column));

// put value
cells.put(CellKey.at(row, column), value);
cells.put(CellKey.at(row, column), Arrays.asList(int1, int2, int3));

// check if value exists
cells.contains(CellKey.at(row, column));

// iterate over all cells containing values
cells.entrySet().stream().forEach(e -> {
    String row = e.getKey().getRow();
    String column = e.getKey().getColumn();
    List<Integer> value = e.getValue();
    // make your actions
});

// iterate over given row
String row = "K";
String[] columns = new String[]{"A", "B", "C", "D"};
for (String c : columns) {
   List<Integer> values = cells.get(CellKey.at(row, c));
   // perform actions
}
Alex Salauyou
  • 14,185
  • 5
  • 45
  • 67