### Question:
DataCompareUtils { public static Map<String, Map<String, Field>> rowListToMap(List<Row> rowList, List<String> primaryKeyList) { Map<String, Map<String, Field>> rowMap = new HashMap<>(); for (Row row : rowList) { List<Field> rowFieldList = row.getFields().stream() .sorted(Comparator.comparing(Field::getName)) .collect(Collectors.toList()); Map<String, Field> colsMap = new HashMap<>(); StringBuilder rowKey = new StringBuilder(); boolean firstUnderline = false; for (int j = 0; j < rowFieldList.size(); j++) { Field field = rowFieldList.get(j); if (primaryKeyList.stream().anyMatch(e -> field.getName().equals(e))) { if (firstUnderline && j > 0) { rowKey.append("_"); } rowKey.append(String.valueOf(field.getValue())); firstUnderline = true; } colsMap.put(field.getName().trim().toUpperCase(), field); } rowMap.put(rowKey.toString(), colsMap); } return rowMap; } private  DataCompareUtils(); static Result<Boolean> isFieldEquals(Field f0, Field f1); static Result<Boolean> isRecordsEquals(TableRecords beforeImage, TableRecords afterImage); static Result<Boolean> isRowsEquals(TableMeta tableMetaData, List<Row> oldRows, List<Row> newRows); static Map<String, Map<String, Field>> rowListToMap(List<Row> rowList, List<String> primaryKeyList);  }### Answer:
@Test public void testRowListToMapWithSinglePk(){ List<String> primaryKeyList = new ArrayList<>(); primaryKeyList.add("id"); List<Row> rows = new ArrayList<>(); Field field = new Field("id", 1, "1"); Row row = new Row(); row.add(field); rows.add(row); Field field2 = new Field("id", 1, "2"); Row row2 = new Row(); row2.add(field2); rows.add(row2); Field field3 = new Field("id", 1, "3"); Row row3 = new Row(); row3.add(field3); rows.add(row3); Map<String, Map<String, Field>> result =DataCompareUtils.rowListToMap(rows,primaryKeyList); Assertions.assertTrue(result.size()==3); Assertions.assertEquals(result.keySet().iterator().next(),"1"); }
@Test public void testRowListToMapWithMultipPk(){ List<String> primaryKeyList = new ArrayList<>(); primaryKeyList.add("id1"); primaryKeyList.add("id2"); List<Row> rows = new ArrayList<>(); Field field1 = new Field("id1", 1, "1"); Field field11 = new Field("id2", 1, "2"); Row row = new Row(); row.add(field1); row.add(field11); rows.add(row); Field field2 = new Field("id1", 1, "3"); Field field22 = new Field("id2", 1, "4"); Row row2 = new Row(); row2.add(field2); row2.add(field22); rows.add(row2); Field field3 = new Field("id1", 1, "5"); Field field33 = new Field("id2", 1, "6"); Row row3 = new Row(); row3.add(field3); row3.add(field33); rows.add(row3); Map<String, Map<String, Field>> result =DataCompareUtils.rowListToMap(rows,primaryKeyList); Assertions.assertTrue(result.size()==3); Assertions.assertEquals(result.keySet().iterator().next(),"1_2"); }