这个时代不会阻止你自己闪耀,但你也覆盖不了任何人的光辉——黄渤
今天发现一种方便的链式Consumer
写法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import lombok.experimental.UtilityClass;
import java.util.function.Consumer; import java.util.stream.Stream;
@UtilityClass public class LambdaHelper {
@SafeVarargs public static <T> Consumer<T> consumers(Consumer<T>... consumers) { return Stream.of(consumers).reduce(Consumer::andThen).orElseGet(() -> o -> {}); } }
|
使用起来:
1 2 3 4 5
| Stream.of("1","2").forEach( LambdaHelper.consumers( System.out::println, log::debug ));
|
结合hutool
的Opt
使用
1 2 3 4 5 6 7 8 9 10 11
| Opt.ofNullable(body.getLevelRules()) .peek(levelRules -> StaticService.saveOrUpdateBatch(levelRules.stream() .flatMap(lambdaRule -> Stream.of(lambdaRule.getQuota(), lambdaRule.getDuration())).collect(Collectors.toList()))) .peek(levelRules -> levelRules.forEach( LambdaHelper.consumers( levelRule -> levelRule.setRuleId(body.getId()), levelRule -> Opt.ofNullable(levelRule).map(LevelRule::getQuota).map(CommonRange::getId).peek(levelRule::setQuotaId), levelRule -> Opt.ofNullable(levelRule).map(LevelRule::getDuration).map(CommonRange::getId).peek(levelRule::setDurationId), levelRule -> Opt.ofNullable(levelRule).map(LevelRule::getAverage).map(CommonRange::getId).peek(levelRule::setAverageId) ))) .peek(StaticService::saveOrUpdateBatch);
|