排序
普通写法
floorList.sort(new Comparator<CommonVo>() {
@Override
public int compare(CommonVo o1, CommonVo o2) {
return o1.getName().compareTo(o2.getName());
}
});
一阶写法:
floorList.sort((CommonVo o1, CommonVo o2) -> o1.getName().compareTo(o2.getName()));
二阶写法
floorList.sort(Comparator.comparing((CommonVo o) -> o.getName()).thenComparing((CommonVo o) -> o.getId()));
创建列表
普通写法:
lambda写法:
List<Human> humans = Lists.newArrayList(new Human("Sarah", 10), new Human("Jack", 12));
list to Map
Long,String类型:
List<Long> brandFormatIds = brandFormatManager.getBrandFormatIdsByBrandId(Long.valueOf(brandId));
if (CollectionUtils.isNotEmpty(brandFormatIds)){
Map<Long, Long> collect = brandFormatIds.stream().collect(Collectors.toMap(e -> e, e -> e, (k1, k2) -> k1));
}
Object,T类型:
List<MallShop> mallShopList = mallShopManager.getMallShopList(mallEntityId);
Map<String,MallShop> map = mallShopList .stream().collect(Collectors.toMap(MallShop::getShopEntityId,mallShop->mallShop,(m1,m2)->m1));
List类型互转:
List contractNos = contractNos1.stream().map(t -> t.toString()).collect(Collectors.toList());
List 获取T中的一个属性集合并去重:
oldBunkIds = oldShopBunkList.stream().map(b -> b.getBunkId()).distinct().collect(Collectors.toList());
List转Map<String, List>:
Map<String, List<String>> autoTagNameListMap = wechatFollowUserTagList.stream()
.filter(wechatFollowUserTag -> FollowUserTagTypeEnums.AUTOMATIC_TAG.getCode().equals(wechatFollowUserTag.getType()))
.collect(Collectors.groupingBy(WechatFollowUserTag::getGroupName, Collectors.mapping(WechatFollowUserTag::getTagName, Collectors.toList())));
空判断:
wechatUserDepDbList = Optional.ofNullable(wechatUserDepDbList).orElseGet(() -> new ArrayList<>());