本人日常工作的一些积累,经常用又容易忘,写个博客记录一下。
//根据Attendance类中的staffCode属性查重,并将重复数据放到repetition集合中
List<String> repetition =new ArrayList<>();
attendanceList.stream().collect(Collectors.groupingBy(Attendance::getStaffCode)).values().stream()
.filter(attendances -> attendances.size() > 1)
.map(attendances -> attendances.get(0))
.forEach(attendance -> repetition.add(attendance.getStaffCode()));
if(!repetition.isEmpty()){
jsonResponseVO.setSuccess(Boolean.FALSE);
jsonResponseVO.setReason("员工工号为" + repetition.get(0) + "重复,请检查!");
return jsonResponseVO;
}
//主要是通过stream中的filter函数以及contains函数做出筛选
final List<Long> employeeIds = payrollCheckRepository.queryCorrectEmpIds(empType, condition.getPayrollCheckViewCode(), checkoutYM);
List<Long> selectedEmployeeIds=payrollCheckRepository.queryOtherEmpIds(Long.valueOf(PAYROLL_CHECK_EMP_SELECT.getRetCode()),condition.getPayrollCheckViewCode(),condition.getCheckoutYM());
if(!selectedEmployeeIds.isEmpty()){
List<Long> ids=employeeIds.stream().filter(item ->selectedEmployeeIds.contains(item)).collect(Collectors.toList());
if(!ids.isEmpty()){
String staffCode=employeeService.queryEmployeeById(ids.get(0)).getStaffCode();
returnInfo.setSuccess(Boolean.FALSE);
returnInfo.setReason("员工工号为"+staffCode+"已存在于其他核算表中,请检查!");
return returnInfo;
}
}
List<String> excelStaffList = attendanceList.stream().map(Attendance::getStaffCode).collect(Collectors.toList());
List<Dependant> dependantList =entities.stream().filter(d -> !StringUtils.isEmpty(d.getDependantName())).collect(Collectors.toList());
List uniqueStr = list.stream().distinct().collect(Collectors.toList());
List<InsuredRule> deleteList = insuredRuleList.stream().collect(
collectingAndThen(
toCollection(() -> new TreeSet<>(Comparator.comparing(InsuredRule::getRuleName))), ArrayList::new)
);
List<Workflow> collect = list.stream().filter(workFlow -> workFlow .getWorkflowName().equals(condition.getWorkflowName())).collect(Collectors.toList());
List<String> list1 = new ArrayList();
list1.add("11");
list1.add("22");
list1.add("33");
List<String> list2 = new ArrayList();
list2.add("33");
list2.add("44");
list2.add("55");
// 交集
List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(toList());
System.out.println("---得到交集 intersection---");
intersection.parallelStream().forEach(System.out :: println);
// 差集 (list1 - list2)
List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(toList());
System.out.println("---得到差集 reduce1 (list1 - list2)---");
reduce1.parallelStream().forEach(System.out :: println);
// 差集 (list2 - list1)
List<String> reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(toList());
System.out.println("---得到差集 reduce2 (list2 - list1)---");
reduce2.parallelStream().forEach(System.out :: println);
// 并集
List<String> listAll = list1.parallelStream().collect(toList());
List<String> listAll2 = list2.parallelStream().collect(toList());
listAll.addAll(listAll2);
System.out.println("---得到并集 listAll---");
listAll.parallelStream().forEach(System.out :: println);
// 去重并集
List<String> listAllDistinct = listAll.stream().distinct().collect(toList());
System.out.println("---得到去重并集 listAllDistinct---");
listAllDistinct.parallelStream().forEach(System.out :: println);
id为key,apple对象为value,可以这么做:
/**
* List -> Map
* 需要注意的是:
* toMap 如果集合对象有重复的key,会报错Duplicate key ....
* 两个对象的id都为1。
* 可以用 (k1,k2)->k1 来设置,如果有重复的key,则保留key1,舍弃key2
*/
Map<Integer, Apple> appleMap = appleList.stream().collect(Collectors.toMap(Apple::getId, a -> a,(k1,k2)->k1));
Map<Integer, List<Apple>> groupBy = appleList.stream().collect(Collectors.groupingBy(Apple::getId));
CopyOnWrite容器即写时复制的容器。通俗的理解是当我们往一个容器添加元素的时候,不直接往当前容器添加,而是先将当前容器进行Copy,
复制出一个新的容器,然后新的容器里添加元素,添加完元素之后,再将原容器的引用指向新的容器。这样做的好处是我们可以对CopyOnWrite容器进行并发的读,
而不需要加锁,因为当前容器不会添加任何元素。所以CopyOnWrite容器也是一种读写分离的思想,读和写不同的容器。
CopyOnWriteArrayList<PayrollCheckView> payrollCheckViews =new CopyOnWriteArrayList<>(viewList);
for (PayrollCheckView payrollCheckView:payrollCheckViews) {
if(StringUtils.isNotEmpty(payrollCheckView.getStaffIds())){
List<Long> staffIds= Arrays.stream(payrollCheckView.getStaffIds().split(",")).map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
if (!staffIds.isEmpty()&&!availableIds.containsAll(staffIds)){
payrollCheckViews.remove(payrollCheckView);
}
}
}