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()));
     
  • 相关阅读:
    AIZU 0005
    Android Studio 1.0 (稳定版) 完全攻略
    android studio中avd sdk路径
    vim强大探究之光标移动
    Android项目打第三方jar包
    Android导出jar包后的资源使用问题
    混淆Android JAR包的方法
    Layout_margn与padding的区别
    Android软件开发之盘点自定义View界面大合集(二)
    Android软件开发之盘点所有Dialog对话框大合集(一)
  • 原文地址:https://www.cnblogs.com/loserchange/p/8317661.html
Copyright © 2011-2022 走看看