public void arrayToMap() {
String tableStr = "db01.dbo.table_00,db02.dbo.table_00,db02.dbo.table_01,db02.dbo2.table_01,db02.dbo2.table_02";
String[] sqlServerArr = tableStr.split(",");
// 利用java8的聚合分组
Map<String, List<String>> sqlServerMap =
Arrays.stream(sqlServerArr)
.collect(Collectors.groupingBy(dbTable ->
StringUtils.substring(dbTable, 0, dbTable.indexOf("."))));
Map<String, List<String>> sqlServerMap2 =
Arrays.stream(sqlServerArr)
.collect(Collectors.groupingBy(dbTable -> StringUtils.substring(dbTable, 0, dbTable.lastIndexOf(".")),
Collectors.mapping(k -> StringUtils.substring(k, k.lastIndexOf(".") + 1, k.length()),
Collectors.toList())));
System.out.println(sqlServerMap.toString());
System.out.println(sqlServerMap2.toString());
输出结果如下:
// sqlServerMap:
{db01=[db01.dbo.table_00], db02=[db02.dbo.table_00, db02.dbo.table_01, db02.dbo2.table_01, db02.dbo2.table_02]}
// sqlServerMap2:
{db02.dbo=[table_00, table_01], db01.dbo=[table_00], db02.dbo2=[table_01, table_02]}