常用方法
ObjectUtil.isEmpty()
比较笼统的判断是否为空,不属于下面类型的仅仅判断是否为null
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public static boolean isEmpty(Object obj) { if (null == obj) { return true; }
if (obj instanceof CharSequence) { return StrUtil.isEmpty((CharSequence) obj); } else if (obj instanceof Map) { return MapUtil.isEmpty((Map) obj); } else if (obj instanceof Iterable) { return IterUtil.isEmpty((Iterable) obj); } else if (obj instanceof Iterator) { return IterUtil.isEmpty((Iterator) obj); } else if (ArrayUtil.isArray(obj)) { return ArrayUtil.isEmpty(obj); }
return false; }
|
Optional.ofNullable().map().orElse()
v
指代是gms
这个列表
1 2 3 4 5 6
| List<AllErgenInfoResp> gms = wsAdpterService.getPatientGmInfo(hospitalNumber); patientTagVO.setIsGm( Optional.ofNullable(gms) .map(v -> !v.isEmpty()) .orElse(false) );
|
equal比较
使用("gcx_blood").equals(param.getObsvCode())
而不是param.getObsvCode().equals()
这样写可以避免空指针异常,如果param.getObsvCode()
返回null
。调用equal()
会抛异常,而使用("gcx_blood").equals(null)
不会抛出异常,而是直接返回 false
。
LocalDateTime
用法
格式化输出
1 2 3 4 5 6 7 8 9 10 11
| package cn.hutool.core.date;
public static String formatNormal(LocalDateTime time) { return format(time, DatePattern.NORM_DATETIME_FORMATTER); }
|
string转LocalDateTime
1 2 3 4 5 6 7 8 9 10 11 12
|
public static LocalDateTime stringToLocalDateTime(String dateTimeString) { LocalDateTime localDateTime = LocalDateTimeUtil.parse(dateTimeString, DatePattern.NORM_DATETIME_PATTERN); return localDateTime; }
|
把map中的值转为list
1 2 3 4 5 6 7 8 9
| Map<String, List<String>> patientMap = new HashMap<>(); patientMap.put("patient1", Arrays.asList("a", "b", "c")); patientMap.put("patient2", Arrays.asList("d", "e")); patientMap.put("patient3", Arrays.asList("f"));
List<String> combinedList = patientMap.values().stream() .flatMap(List::stream) .collect(Collectors.toList());
|
获取一天的开始和结束时间
1 2
| LocalDateTime startOfDay = LocalDateTime.now().with(LocalTime.MIDNIGHT).minusMinutes(10); LocalDateTime endOfDay = LocalDateTime.now().with(LocalTime.MAX);
|