Java 8中stream相关用法

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

1.list转换map

Map<Integer, String> mappedMovies = ghslist.stream().collect( Collectors.toMap(SaleStaticsGhsResp::getShop_saleman_id, (p) -> p.getName()));

2.从实体中取出id集合,去重

List<Integer> 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<SpreadActivityResp> comparator0 =(b, a) -> a.getCreate_time().compareTo(b.getCreate_time());

resultList.sort(comparator0);

Comparator<SpreadActivityResp> 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<Price> pricelist = pricesList.stream().filter(k -> {return skuListId.contains(k.getSku_id());}).collect(Collectors.toList());

List<SearchOrderGoodsResp> unzpList = list_order_goods.stream().filter(m->m.getIs_giveaway() == 0).distinct().collect(Collectors.toList());

 

    原文作者:yinghuananhai
    原文地址: https://blog.csdn.net/YingHuaNanHai/article/details/81273059
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞