您的位置 首页 java

Lambda实践总结

这都快1202年,对Lambda依然一知半解,实在惭愧。最近搬砖中着重实践了一下,总结些实践,贴出如下,留作备忘。

1、基础查询封装

这个封装,主要是用来抽象数据库查询,美化代码的(强迫症)。

函数接口

   
 @FunctionalInterface
public interface Query<T> {
  List<T>  Query () throws  Exception ;
}

  

工具类

   
 public class QueryUtil {
  public  static  <T> T query(Query<T> query) throws Exception {
    List<T> list = query.query();
    if (null != list && list.size() > 0) {
      return list.get(0);
    }
    return null;
  }


  public static <T> List<T> queryList(Query<T> query) throws Exception {
    List<T> list = query.query();
    if (null != list && list.size() > 0) {
      return list;
    }
    return new ArrayList<T>();
  }
}  

2、遍历

遍历可以直接 forEach 也可以先stream()再forEach

 #直接foreach
  
 departmentVos.forEach(departmentVo -> {
      ids.add(departmentVo.getOaDeptId());
      pids.add(departmentVo.getOaDeptParentId());
      tmp.put(departmentVo.getOaDeptParentId(), departmentVo);
    });  
 #先stream与上等效

  
 departmentVos.stream().forEach(departmentVo -> {
      ids.add(departmentVo.getOaDeptId());
      pids.add(departmentVo.getOaDeptParentId());
      tmp.put(departmentVo.getOaDeptParentId(), departmentVo);
    });  
 #特殊情况可以开启多线程

  
 departmentVos.parallelStream().forEach(departmentVo -> {
      ids.add(departmentVo.getOaDeptId());
      pids.add(departmentVo.getOaDeptParentId());
      tmp.put(departmentVo.getOaDeptParentId(), departmentVo);
    });

  

3、调用工具类实现数据库查询

这个属于自己定义的函数接口

 #单条
  
  RSync DeptPosiEmplVo rSyncDeptPosiEmplVo = QueryUtil.query(() -> {
        RSyncDeptPosiEmplVo rSyncDeptPosiEmplQc = new RSyncDeptPosiEmplVo();
        rSyncDeptPosiEmplQc.setOaUserId(currentEmpl.getOaUserId());
        rSyncDeptPosiEmplQc.setIsOn(( byte ) 1);
        rSyncDeptPosiEmplQc.setIsSeveralPositions((byte) 0);
        return this.irSyncDeptPosiEmplService.queryRSyncDeptPosiEmpl(rSyncDeptPosiEmplQc);
      });  
 #多条

  
 List<RSyncDeptPosiEmplVo> rSyncDeptPosiEmplVos = QueryUtil.queryList(() -> {
        RSyncDeptPosiEmplVo rSyncDeptPosiEmplQc = new RSyncDeptPosiEmplVo();
        rSyncDeptPosiEmplQc.setOaUserId(currentEmpl.getOaUserId());
        rSyncDeptPosiEmplQc.setIsOn((byte) 1);
        rSyncDeptPosiEmplQc.setIsSeveralPositions((byte) 0);
        return this.irSyncDeptPosiEmplService.queryRSyncDeptPosiEmpl(rSyncDeptPosiEmplQc);
      });  
   

4、 ETL

姑且叫ETL吧

 #原样输出(主要用于中间数据处理)
  
 deptList.stream().map((dept) -> {
      return this.fillTreeData(dept, oaDeptIds);
    }).collect(Collectors.toList())  
 #过滤(如过滤掉null数据

  
 deptList.stream().filter((dept) -> {
      return null==dept;
    }).collect(Collectors.toList())  
 #转换

  
 deptList.stream().map(RSyncDeptPosiEmplVo::getId).collect(Collectors.toList())

  

5、计算

直接上码

   
 #加
teacherVo.getTotalTeachHour()
              + rClassSubjectVos.stream().mapToInt(RClassSubjectVo::getSubHour).sum()
#平均
teacherVo.getTotalTeachHour()
              + rClassSubjectVos.stream().mapToInt(RClassSubjectVo::getSubHour).average()
#其它还有减、除、统计数量等等

  

6、判断

   
 permissionVos.stream().anyMatch(p -> p.getIsApproval().equals((byte) 1)

  

7、搜索匹配

可以搜索符合条件的,也可以搜索符合条件的第一个

   
 permissionVos.stream()
        .filter(p -> p.getDownSize() > 0 && p.getDownSize() > p.getRealDownSize()).findFirst();

  

暂时就这么多了,想起来再补充。

文章来源:智云一二三科技

文章标题:Lambda实践总结

文章地址:https://www.zhihuclub.com/201251.shtml

关于作者: 智云科技

热门文章

网站地图