java8新特性lambda和集合搭配使用,在一定程度上使代码更加简洁,能用少量代码实现了一个方法的功能,特别是搭配Hutool(糊涂工具)使用的时候。对于新手可能阅读上有些障碍,但整个团队能够熟悉应用的情况下,值得大量推广。以下是平时的一些实例应用,希望能够从中得到些启发: 匹配集合元素,对匹配上的设置值为true
public List<SysCountry> doSysCountrySelected(List<SysCountry> sysCountryList, String countrySelected) {
if (StringUtils.isNotBlank(countrySelected)) {
String[] selectedArr = countrySelected.split(",");
//
sysCountryList.forEach(country -> {
if (ArrayUtil.contains(selectedArr, country.getCountryCode())) {
country.setFlag(true);
}
});
}
return sysCountryList;
}
对列表数据组织成某种格式串
List<SysAttachment> sysAttachments = sysAttachmentService.selectSysAttachmentList(sysAttachment);
//处理成#id1##id2#
fileIds = sysAttachments.stream().map(att -> "#" + att.getAttId() + "#").collect(Collectors.joining());
两个列表的元素进行遍历匹配
public boolean checkProductCountryExisted(BasicProductZone basicProductZone) {
BasicProductZone productZone = new BasicProductZone();
productZone.setProductId(basicProductZone.getProductId());
List<BasicProductZone> zoneList = basicProductZoneMapper.selectBasicProductZoneList(productZone);
String[] countryArr = basicProductZone.getCountryCode().split(",");
boolean match = zoneList.stream().anyMatch(zone -> {
for (String country : countryArr) {
if (zone.getCountryCode().contains(country)) {
//国家已经存在(使用过了)
return true;
}
}
//国家没被用过
return false;
});
return match;
}
未完待续.....