一些开发的常识

代码上加上这句话的都是代码有洁癖

@SupressWarnings("unchecked")

去除这段代码也就是把Autowired 干掉
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DesensitizeRuleController {

    private final SampleDataService sampleDataService;

}

争对一个需求 如果前端就是一个输入窗口,然后后端多个字段要匹配上,那么可以定义一个注解,然后在每个字段上加上这个注解,定义一个searchWrapper,然后searchmapper拼接查询条件

1种很好的检验值判断,当这个值判断不存在的时候 那么自己会返回一个状态码,这样前端会自动获取这个值,而不需要自动获得,每一个往上面传,麻烦,代码耦合严重,这种写法参考google的
模仿 guava 的 Preconditions
* 

* 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 codeCountMap = codes.stream().collect(Collectors.groupingBy(e -> e, Collectors.counting()));
List cnNameRepeatInExcel = codeCountMap.entrySet().stream().filter(e -> e.getValue() > 1).map(Map.Entry::getKey).collect(Collectors.toList());

合并2个list方法 这样一般数据库一个join 解决

//以id为key值,将list转换为Map Map userExtendMap = userExtends.stream() .collect(Collectors.toMap(UserExtend::getId, userExtend -> userExtend));

  1. 通过流的方式拼接对象
  2. List mapList = new ArrayList<>();
    Map> mapListGroup = mapList.stream().
            collect(Collectors.groupingBy(e->e.get("xxx").toString()));
     
        //通过流的方式拼接两个对象成为新对象
        List 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 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 sysUsers1 = JSON.parseArray(JSON.toJSONString(sysUsers), SysUser.class);
//    sysUsers.remove(1);
      System.out.println(JSON.toJSONString(sysUsers));
      System.out.println(JSON.toJSONString(sysUsers1));
      Map collect = sysUsers.stream().collect(Collectors.toMap(
                  SysUser::getId, Function.identity(), BinaryOperator.maxBy(
                        Comparator.comparing(SysUser::getLoginTenantId))));
      System.out.println(JSON.toJSONString(collect));
List collect1 = sysUsers.stream().filter(e -> collect.containsKey(e.getId())).collect(Collectors.toList());
System.out.println(JSON.toJSONString(collect1));

2.2个list取交集

List intersect = list1.stream().filter(item -> list2.contains(item)).collect(Collectors.toList());

你可能感兴趣的:(java,开发语言)