Java 8中stream相关用法

jdk1.8新加了lambda表达式,可以帮助开发者更好的,更高效的实现一些通常复杂的代码实现

1.list转换map

Map mappedMovies = ghslist.stream().collect( Collectors.toMap(SaleStaticsGhsResp::getShop_saleman_id, (p) -> p.getName()));
2.从实体中取出id集合,去重

List list = page.stream().filter(m->m.getId()>0).map(k -> k.getId()).distinct().collect(Collectors.toList());
3.遍历一个list,和另一个list对比,取出id相同的每个实体

SaleMan sale = saleList.stream().filter(e -> e.getId().intValue() == olm.getSm_saleman_id()).findFirst().orElse(null);
4.排序(先按第一个字段排序,如果排序字段相等,在按第二个字段排序)

Comparator comparator0 =(b, a) -> a.getCreate_time().compareTo(b.getCreate_time());
 
resultList.sort(comparator0);
 
Comparator comparator = comparator0.thenComparing((a,b) -> b.getCreate_time().compareTo(a.getCreate_time()));
 
for (CartGoodsInfo info : shop.getGoodsInfoList()) {
 
    cartList.add(info.getCartInfo());
 
    info.getPriceInfoList().sort((a, b) -> {
 
    return a.getMinCount().compareTo(b.getMinCount());
 
});
 
priceList.addAll(info.getPriceInfoList());
 
}
 
list_order_goods.sort((a,b)->{return Integer.valueOf(a.getId()).compareTo(b.getId());});
5.根据某个id获取list中和这个id相同的实体

List pricelist = pricesList.stream().filter(k -> {return skuListId.contains(k.getSku_id());}).collect(Collectors.toList());
 
List unzpList = list_order_goods.stream().filter(m->m.getIs_giveaway() == 0).distinct().collect(Collectors.toList());
 
--------------------- 
作者:yinghuananhai 
来源:CSDN 
原文:https://blog.csdn.net/YingHuaNanHai/article/details/81273059 
版权声明:本文为博主原创文章,转载请附上博文链接!

你可能感兴趣的:(Java)