Lambda practice summary

Keywords: Programming Database Lambda

It's almost 0202 years ago. I still know a little about Lambda. I'm really ashamed. Recently, we have put emphasis on the practice of brick handling, summarized some practices, and posted the following as a note.

1. Basic query encapsulation

This encapsulation is mainly used to abstract database queries and beautify code (obsessive-compulsive disorder).

Function interface

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

Tool class

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. Traversal

Traversal can be done either directly for each or first stream() and then for each

#Direct foreach
departmentVos.forEach(departmentVo -> {
			ids.add(departmentVo.getOaDeptId());
			pids.add(departmentVo.getOaDeptParentId());
			tmp.put(departmentVo.getOaDeptParentId(), departmentVo);
		});
#stream first and upper equivalence
departmentVos.stream().forEach(departmentVo -> {
			ids.add(departmentVo.getOaDeptId());
			pids.add(departmentVo.getOaDeptParentId());
			tmp.put(departmentVo.getOaDeptParentId(), departmentVo);
		});
#Multithreading can be enabled in special cases
departmentVos.parallelStream().forEach(departmentVo -> {
			ids.add(departmentVo.getOaDeptId());
			pids.add(departmentVo.getOaDeptParentId());
			tmp.put(departmentVo.getOaDeptParentId(), departmentVo);
		});

3. Calling tool class to realize database query

This is a self defined function interface

#Single strip
RSyncDeptPosiEmplVo rSyncDeptPosiEmplVo = QueryUtil.query(() -> {
				RSyncDeptPosiEmplVo rSyncDeptPosiEmplQc = new RSyncDeptPosiEmplVo();
				rSyncDeptPosiEmplQc.setOaUserId(currentEmpl.getOaUserId());
				rSyncDeptPosiEmplQc.setIsOn((byte) 1);
				rSyncDeptPosiEmplQc.setIsSeveralPositions((byte) 0);
				return this.irSyncDeptPosiEmplService.queryRSyncDeptPosiEmpl(rSyncDeptPosiEmplQc);
			});
#Multiple strips
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

Let's call it ETL

#As is output (mainly used for intermediate data processing)
deptList.stream().map((dept) -> {
			return this.fillTreeData(dept, oaDeptIds);
		}).collect(Collectors.toList())
#Filtering (e.g. filtering out null data
deptList.stream().filter((dept) -> {
			return null==dept;
		}).collect(Collectors.toList())
#Transformation
deptList.stream().map(RSyncDeptPosiEmplVo::getId).collect(Collectors.toList())

5. Calculation

Direct up code

#plus
teacherVo.getTotalTeachHour()
							+ rClassSubjectVos.stream().mapToInt(RClassSubjectVo::getSubHour).sum()
#Average
teacherVo.getTotalTeachHour()
							+ rClassSubjectVos.stream().mapToInt(RClassSubjectVo::getSubHour).average()
#Others include subtraction, division, statistics, etc

6. Judgment

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

7. Search matching

You can search for the one that meets the criteria or the first one that meets the criteria

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

That's all for now. I'll add it when I think of it.

Posted by JeanieTallis on Thu, 28 Nov 2019 08:24:39 -0800