用了Stream后,代码反而越写越丑?
Java后端技术
共 7746字,需浏览 16分钟
·
2022-04-28 21:57
往期热门文章:
1、一不小心节约了 591 台机器! 2、你见过哪些目瞪口呆的 Java 代码技巧? 3、笑死!程序员延寿指南开源了 4、互联网黑话,被上海人翻译火了 5、还在用分页?太Low !试试 MyBatis 流式查询,真心爽!
合理的换行
舍得拆分函数
合理的使用 Optional
返回 Stream 还是返回 List?
少用或者不用并行流
总结
public List getFeeds(Query query,Page page) {
ListorgiList = new ArrayList<>();
Listcollect = page.getRecords().stream()
.filter(this::addDetail)
.map(FeedItemVo::convertVo)
.filter(vo -> this.addOrgNames(query.getIsSlow(),orgiList,vo))
.collect(Collectors.toList());
//...其他逻辑
return collect;
}
private boolean addDetail(FeedItem feed){
vo.setItemCardConf(service.getById(feed.getId()));
return true;
}
private boolean addOrgNames(boolean isSlow,ListorgiList,FeedItemVo vo) {
if(isShow && vo.getOrgIds() != null){
orgiList.add(vo.getOrgiName());
}
return true;
}
if (!CollectionUtils.isEmpty(roleNameStrList) && roleNameStrList.contains(REGULATORY_ROLE)) {
vos = vos.stream().filter(
vo -> !CollectionUtils.isEmpty(vo.getSpecialTaskItemVoList())
&& vo.getTaskName() != null)
.collect(Collectors.toList());
} else {
vos = vos.stream().filter(vo -> vo.getIsSelect()
&& vo.getTaskName() != null)
.collect(Collectors.toList());
vos = vos.stream().filter(
vo -> !CollectionUtils.isEmpty(vo.getSpecialTaskItemVoList())
&& vo.getTaskName() != null)
.collect(Collectors.toList());
}
result.addAll(vos.stream().collect(Collectors.toList()));
合理的换行
Stream.of("i", "am", "xjjdog").map(toUpperCase()).map(toBase64()).collect(joining(" "));
Stream.of("i", "am", "xjjdog")
.map(toUpperCase())
.map(toBase64())
.collect(joining(" "));
舍得拆分函数
public Stream getOrderByUser(String userId){
return orderRepo.findOrderByUser().stream()
.map(order-> {
OrderDto dto = new OrderDto();
dto.setOrderId(order.getOrderId());
dto.setTitle(order.getTitle().split("#")[0]);
dto.setCreateDate(order.getCreateDate().getTime());
return dto;
});
}
public Stream getOrderByUser(String userId){
return orderRepo.findOrderByUser().stream()
.map(this::toOrderDto);
}
public OrderDto toOrderDto(Order order){
OrderDto dto = new OrderDto();
dto.setOrderId(order.getOrderId());
dto.setTitle(order.getTitle().split("#")[0]);
dto.setCreateDate(order.getCreateDate().getTime());
return dto;
}
public Stream getOrderByUser(String userId){
return orderRepo.findOrderByUser().stream()
.map(OrderDto::new);
}
Predicate registarIsCorrect = reg ->
reg.getRegulationId() != null
&& reg.getRegulationId() != 0
&& reg.getType() == 0;
合理的使用 Optional
if(null == obj)
if(null == user.getName() || "".equals(user.getName()))
if (order != null) {
Logistics logistics = order.getLogistics();
if(logistics != null){
Address address = logistics.getAddress();
if (address != null) {
Country country = address.getCountry();
if (country != null) {
Isocode isocode = country.getIsocode();
if (isocode != null) {
return isocode.getNumber();
}
}
}
}
}
String result = Optional.ofNullable(order)
.flatMap(order->order.getLogistics())
.flatMap(logistics -> logistics.getAddress())
.flatMap(address -> address.getCountry())
.map(country -> country.getIsocode())
.orElse(Isocode.CHINA.getNumber());
public Optional getUserName() {
return Optional.ofNullable(userName);
}
Optional<String> userName = "xjjdog";
String defaultEmail = userName.get() == null ? "":userName.get() + "@xjjdog.cn";
Optional<String> userName = "xjjdog";
String defaultEmail = userName
.map(e -> e + "@xjjdog.cn")
.orElse("");
返回 Stream 还是返回 List?
public Stream getAuthUsers() {
...
return Stream.of(users);
}
少用或者不用并行流
List transform(List source){
List dst = new ArrayList<>();
if(CollectionUtils.isEmpty()){
return dst;
}
source.stream.
.parallel()
.map(..)
.filter(..)
.foreach(dst::add);
return dst;
}
总结
<dependency>
<groupId>io.vavrgroupId>
<artifactId>vavrartifactId>
<version>0.10.3version>
dependency>
往期热门文章:
1、笑死!程序员延寿指南开源了 2、用 Dubbo 传输文件?被老板一顿揍! 3、45 个 Git 经典操作场景,专治不会合代码! 4、@Transactional 注解失效的3种原因及解决办法 5、小学生们在B站讲算法,网友:我只会阿巴阿巴 6、Spring爆出比Log4j2还大的漏洞? 7、Objects.equals 有坑!!! 8、Redis 官方可视化工具,功能真心强大! 8、xxl-job 和 ElasticJob比,谁牛? 10、推荐好用的 Spring Boot 内置工具类
评论