java stream处理

List<User> list = new ArrayList<>();
1、list转Map<String, String>

Map<String, String> resultMap = list.stream().collect(Collectors.toMap(User::getName, User::getOtherName));

2、list转Map<String, Set<String>>
Map<String, Set<String>> resultMap= list.stream().collect(Collectors.groupingBy(e -> e.getCategory() + "_" + e.getVersion(),
                Collectors.mapping(User::getName, Collectors.toSet())));
Map<String, List<String>> resultMap= list.stream().collect(Collectors.groupingBy(e -> e.getCategory() + "_" + e.getVersion(),
                Collectors.mapping(User::getEventName, Collectors.toList())));

3、list转Map<String, List<User>>
Map<String, List<User>> resultMap = list.stream().collect(Collectors.groupingBy(User::getName));

Map<String, List<Map>> resultMap = list.stream().collect(Collectors.groupingBy(e->e.get("entityName").toString()));

3.1 list 转换字段 以及对应的数量
Map<String, Long> mapProperty = list.stream()
                .collect(Collectors.groupingBy(User::getName, Collectors.counting()));

4、list转Map<String, User>
 Map<String, User> resultMap = list.stream().collect(Collectors.toMap(i ->
                i.getName(), Function.identity(), (k1, k2) -> k2));
5、list转List<String>
List<String> events = list.stream().map(User::getName).collect(Collectors.toList());
去重
List<String> events = list.stream().map(User::getName).distinct().collect(Collectors.toList());
排序
List<String> events = list.stream().map(User::getName).distinct().sorted().collect(Collectors.toList());
6、list转Set<String>
Set<String> events = list.stream().map(e -> e.getName()).collect(Collectors.toSet());
 Set<Integer> forecastModelIds =  list.stream().map(User::getId).collect(Collectors.toSet());
带排序
Set<String> newData = list.getLabelDetails().stream().map(e -> e.getEventName())
                .sorted().collect(Collectors.toCollection(LinkedHashSet::new));
7、对List<User> 中entityCategory进行分组,count进行求和
Map<String, Integer> result = list.stream()
                .collect(Collectors.groupingBy(User::getName,
                        Collectors.summingInt(User::getCount)));
8、对List<User>中字段count求和
Integer total = entityClassyDetail.stream().mapToInt(User::getCount).sum();


9.取id对应最小的日期
Map<Integer, Data> result = dataList.stream()
                .collect(Collectors.groupingBy(Data::getId,
                        Collectors.minBy(Comparator.comparing(Data::getDate))));

10、list去重
List<String> list = List.of("apple", "banana", "apple", "orange", "banana", "pear");
List<String> distinctList = list.stream().distinct().collect(Collectors.toList());


#if($_parameter.labelSecondaryName.contains("-"))
    AND  ( f.LABEL_SECONDARY_NAME IS NULL 
        #if($!{_parameter.labelSecondaryName.size()}>1)
            OR f.LABEL_SECONDARY_NAME IN (#repeat( $_parameter.labelSecondaryName $item ",") @{item}#end)
        #end
        )
#end
#if(!$_parameter.labelSecondaryName.contains("-"))
    AND f.LABEL_SECONDARY_NAME IN (#repeat( $_parameter.labelSecondaryName $item ",") @{item}#end)
#end


List<String> mips = Collections.singletonList("1");
mips = Arrays.asList("1","2");

excel 
字符串拼接
=CONCATENATE("UPDATE tableName SET RULE_NAME='",B49,"' WHERE RULE_NAME='",A49,"';")
=CONCATENATE("('",A2,"','",B2,"','",C2,"','",D2,"','",E2,"','",F2,"','",G2,"','",H2,"','",I2,"','",J2,"'),")


/*+ SET_VAR(query_timeout = 36000)*/

循环map
Map<Integer, List<Object>> forecastMap = 
for (Map.Entry<Integer, List<Object>> i : forecastMap.entrySet()) {
            
}

输出时间
DateUtil.spendMs(start)

-- 查询表字段

SELECT 
    GROUP_CONCAT(CONCAT('`',COLUMN_NAME,'`')) 
FROM 
    information_schema.COLUMNS 
WHERE 
    TABLE_SCHEMA = 'dbName' 
    AND TABLE_NAME = 'tableName';
    
SELECT 
    GROUP_CONCAT(CONCAT('SUM(`',COLUMN_NAME,'`) ',COLUMN_NAME)) 
FROM 
    information_schema.COLUMNS 
WHERE 
    TABLE_SCHEMA = 'dbName' 
    AND TABLE_NAME = 'tableName';

SELECT 
    CONCAT(',t.`',COLUMN_NAME,'`=w.',COLUMN_NAME)
FROM 
    information_schema.COLUMNS 
WHERE 
    TABLE_SCHEMA = 'dbName' 
    AND TABLE_NAME = 'tableName';
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值