class Crosstab {

    public static Crosstab convert(Map<Tuple2<String, String>, Long> maps) {
        Crosstab crosstab = new Crosstab();

        //get row tags and col tags
        Set<Tuple2<String, String>> sets = maps.keySet();

        Set<String> rowTags = new HashSet<>();
        Set<String> colTags = new HashSet<>();
        for (Tuple2<String, String> tuple2 : sets) {
            rowTags.add(tuple2.f0);
            colTags.add(tuple2.f1);
        }

        crosstab.rowTags = new ArrayList<>(rowTags);
        crosstab.colTags = new ArrayList<>(colTags);

        int rowLen = crosstab.rowTags.size();
        int colLen = crosstab.colTags.size();

        //compute value
        crosstab.data = new long[rowLen][colLen];
        for (Map.Entry<Tuple2<String, String>, Long> entry : maps.entrySet()) {
            int rowIdx = crosstab.rowTags.indexOf(entry.getKey().f0);
            int colIdx = crosstab.colTags.indexOf(entry.getKey().f1);
            crosstab.data[rowIdx][colIdx] = entry.getValue();
        }
        return crosstab;
    }

}