Crosstab { public static Crosstab merge(Crosstab left, Crosstab right) { Crosstab crosstab = new Crosstab(); Set<String> rowTags = new HashSet<>(left.rowTags); rowTags.addAll(right.rowTags); Set<String> colTags = new HashSet<>(left.colTags); colTags.addAll(right.colTags); crosstab.rowTags = new ArrayList<>(rowTags); crosstab.colTags = new ArrayList<>(colTags); int rowLen = crosstab.rowTags.size(); int colLen = crosstab.colTags.size(); crosstab.data = new long[rowLen][colLen]; int i = 0; for (String row : crosstab.rowTags) { int j = 0; for (String col : crosstab.colTags) { long tmp = 0; if (left.rowTags.contains(row) && left.colTags.contains(col)) { tmp += left.data[left.rowTags.indexOf(row)][left.colTags.indexOf(col)]; } if (right.rowTags.contains(row) && right.colTags.contains(col)) { tmp += right.data[right.rowTags.indexOf(row)][right.colTags.indexOf(col)]; } crosstab.data[i][j] = tmp; j++; } i++; } return crosstab; }  static Crosstab convert(Map<Tuple2<String, String>, Long> maps); static Crosstab merge(Crosstab left, Crosstab right); double[] rowSum(); double[] colSum(); double sum(); public List<String> colTags; public List<String> rowTags; public long[][] data; }
@Test public void merge() { Map<Tuple2<String, String>, Long> maps = new HashMap<>(); maps.put(Tuple2.of("f0", "f1"), 2L); maps.put(Tuple2.of("f0", "f2"), 3L); maps.put(Tuple2.of("f1", "f2"), 4L); maps.put(Tuple2.of("f2", "f3"), 5L); Crosstab crossTable = Crosstab.convert(maps); Crosstab crossTableCom = Crosstab.merge(crossTable, crossTable); List<String> rowTags = crossTable.rowTags; List<String> colTags = crossTable.colTags; assertEquals(6.0, crossTableCom.data[rowTags.indexOf("f0")][colTags.indexOf("f2")], 10e-4); }