java8对一些集合的优化操作

1.集合实体提取里面提起一个字段生成新的集合

 List<Shop> shops = shopService.findList(searchParams);

 List<Long> ids = shops.stream().map(Shop::getShopId).collect(Collectors.toList());

2.给里面里面每个实体设置值

List<Banner> list2 = list.getList();

list2.forEach(t -> t.setBannerId(1L));

3.集合的过滤生成新数组

List<Integer> transactionsIds = transactions.parallelStream().
 filter(t -> t.getType() == Transaction.GROCERY).
 sorted(comparing(Transaction::getValue).reversed()).
 map(Transaction::getId).
 collect(toList());
4.集合的排序
 List<RiderForm> datas = daydatas.stream().sorted(Comparator.comparing(RiderForm::getRiderName))
                .collect(Collectors.toList());
 
public class RiderForm implements Serializable, Comparable<RiderForm> {
   @Override
    public int compareTo(
            RiderForm o) {
        return riderName.compareTo(o.getRiderName());

    }  
   @Override
    public boolean equals(
            final Object obj) {
        if (obj == null) {
            return false;
        }
        final RiderForm rf = (RiderForm) obj;
        if (this == rf) {
            return true;
        } else {
            return (this.riderName.equals(rf.riderName) && (this.orderType == rf.orderType));
        }
    }
 @Override
    public int hashCode() {
        int hashno = 7;
        hashno = 13 * hashno + (riderName == null ? 0 : riderName.hashCode());
        return hashno;
    }
 
}
5.遍历map
map.entrySet().forEach(entry -> System.out.println("key:value = " + entry.getKey() + ":" + entry.getValue()));
 
posted @ 2018-01-19 16:32  小虾米丶  阅读(292)  评论(0编辑  收藏  举报