1.各类型判空
字符串类型(String):
str.isBlank()
对象类型(obj):
Objects.isNull()obj != null
集合类型(Map,List, Set) :
CollectionUtils.isEmpty(ages)list.isEmpty()
2.equals()规范
常量放前面 "AAA".equals(e.getName())
3.stream流
stream流 不会改变 数据本身 ,只能通过赋值去获取对数据进行处理和转换后的值
Map<Integer,PersonMo> personMap = map.entrySet().stream() .filter( e -> "BBB".equals(e.getValue().getName())) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));在forEach流后可以用BeanUtils.copyProperties()去获取值
person.stream().forEach(e -> { if ("AAA".equals(e.getName())){ e.setSex("BigFly"); BeanUtils.copyProperties(e,personMo); } });
4. forEach结束循环
for()循环可以用 return break continue退出循环
forEach()不能用 ,只能用 抛异常退出循环
java循环尽量用for循环,js循环用for of循环
List<PersonMo> list = new ArrayList<>(Arrays.asList(new PersonMo("ttt","man",11,null) ,new PersonMo("AAA","woman",111),new PersonMo("BBB","woman",222)));public List<PersonMo> forEachError(List<PersonMo> list) { try { list.forEach(e -> { if ("AAA".equals(e.getName())) throw new Error("退出循环"); System.out.println(e); }); }catch (Error e){ System.out.println(e); return list; } return list; }
5. myBatisPlus用法
通过 方法引用 的方式来使用实体字段名,避免直接写数据库字段名称
LambdaQueryWrapper<CContractfeeEO> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(CContractfeeEO::getAmount,'2');QueryWrapper<CContractfeeEO> queryWrapper = new QueryWrapper(); queryWrapper.lambda().eq(CContractfeeEO::getAmount,'2');
6.多态
List<DayMo> list = new ArrayList<>(); 向上转型,list拥有ArrayList中和List相同的的重写方法和List中未重写的方法,但无法引用ArrayList特有的方法 即lsit可以使用子类中覆盖或新增的方法,但无法直接访问子类特有的方法。
7. Enum类型循环
for (ClassNameEnum className : ClassNameEnum.values()){ if (className.getClassName().equals(studentsMO.getClassName())){ return className.getClassRoom(); } }