代码上加上这句话的都是代码有洁癖
@SupressWarnings("unchecked")
去除这段代码也就是把Autowired 干掉 @RequiredArgsConstructor(onConstructor = @__(@Autowired)) public class DesensitizeRuleController {private final SampleDataService sampleDataService;
}
争对一个需求 如果前端就是一个输入窗口,然后后端多个字段要匹配上,那么可以定义一个注解,然后在每个字段上加上这个注解,定义一个searchWrapper,然后searchmapper拼接查询条件
1种很好的检验值判断,当这个值判断不存在的时候 那么自己会返回一个状态码,这样前端会自动获取这个值,而不需要自动获得,每一个往上面传,麻烦,代码耦合严重,这种写法参考google的
模仿 guava 的 Preconditions * <p> * com.google.common.base.Preconditions
public static void checkState(boolean condition, HttpStatus httpStatus, String msg) throws BusinessException {if (condition) {log.error(msg);throw (httpStatus != null)? new BusinessException(httpStatus, msg): new BusinessException(msg);} }
//其实这2句话就是一个for循环,只不过人家用一个jdk1.8一次性代替了
Map<String, Long> codeCountMap = codes.stream().collect(Collectors.groupingBy(e -> e, Collectors.counting())); List<String> cnNameRepeatInExcel = codeCountMap.entrySet().stream().filter(e -> e.getValue() > 1).map(Map.Entry::getKey).collect(Collectors.toList());
合并2个list方法 这样一般数据库一个join 解决
//以id为key值,将list转换为Map Map<String, UserExtend> userExtendMap = userExtends.stream() .collect(Collectors.toMap(UserExtend::getId, userExtend -> userExtend));
- 通过流的方式拼接对象
- List<Map> mapList = new ArrayList<>();
Map<String,List<Map>> mapListGroup = mapList.stream().
collect(Collectors.groupingBy(e->e.get("xxx").toString()));
//通过流的方式拼接两个对象成为新对象List<UserDetail> userDetails = userList.stream().map(a->{UserDetail userDetail = new UserDetail();userDetail.setId(a.getId());userDetail.setName(a.getName());userDetail.setAge(a.getAge());userDetail.setWork(userExtendMap.get(a.getId()).getWork());return userDetail;}).collect(Collectors.toList());
1.一组对象中获取其中一个属性最大的值的对象 ArrayList<SysUser> sysUsers = new ArrayList<>();SysUser sysUser = new SysUser();sysUser.setEmail("11");sysUser.setId("1111");sysUser.setLoginTenantId(5);sysUsers.add(sysUser);SysUser sysUser1 = new SysUser();sysUser1.setId("1111");sysUser1.setLoginTenantId(5);sysUser1.setEmail("12");sysUsers.add(sysUser1);List<SysUser> sysUsers1 = JSON.parseArray(JSON.toJSONString(sysUsers), SysUser.class); // sysUsers.remove(1);System.out.println(JSON.toJSONString(sysUsers));System.out.println(JSON.toJSONString(sysUsers1));Map<String, SysUser> collect = sysUsers.stream().collect(Collectors.toMap(SysUser::getId, Function.identity(), BinaryOperator.maxBy(Comparator.comparing(SysUser::getLoginTenantId))));System.out.println(JSON.toJSONString(collect));
List<SysUser> collect1 = sysUsers.stream().filter(e -> collect.containsKey(e.getId())).collect(Collectors.toList()); System.out.println(JSON.toJSONString(collect1));
2.2个list取交集
List<String> intersect = list1.stream().filter(item -> list2.contains(item)).collect(Collectors.toList());