zoukankan      html  css  js  c++  java
  • 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()));
     
  • 相关阅读:
    CE工具里自带的学习工具--第三关
    CE工具里自带的学习工具--第二关
    CE工具里自带的学习工具--第一关
    双向链表
    双向循环链表
    单向循环链表
    单链表
    动手动脑———异常处理
    动手动脑——继承与多态
    类与对象——动手动脑
  • 原文地址:https://www.cnblogs.com/loserchange/p/8317661.html
Copyright © 2011-2022 走看看