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()));
     
  • 相关阅读:
    装饰器
    闭包函数
    名称空间与作用域
    函数参数 函数对象 函数嵌套
    文件内光标的移动 函数基础 定义函数的三种形式 函数的返回值 调用方式
    文件
    字符编码 文件处理
    人月神话之阅读笔记一
    mysql+servlet+jsp实现数据库的增删改查
    文件与流课后作业
  • 原文地址:https://www.cnblogs.com/loserchange/p/8317661.html
Copyright © 2011-2022 走看看