java8集合类聚合GroupingBy的妙用

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]}
上一篇:创建数据库Web Services


下一篇:shaded jar中包冲突定位