其他
各种陷进,被 JDK 方法,给坑哭了!!
1. String.valueOf()方法的陷阱
Map<String, Object> userInfo = userService.getUserInfoById(userId);
Object userNameObject = userInfo.get("name");
String userName = String.valueOf(userNameObject);
// 判空
if(userName!=null&&userName.length()>0) {
String message = getMessage(userName);
smsService.send(message);
}
2. Integer.parseInt()方法很矫情
3. Bigdecimal的除法坑你没商量
4. Collections.emptyList()此list非彼list
List<String> resultList = Collections.emptyList();
try {
resultList = userDao.getUserName(userId);
} catch (Exception ex) {
logger.info(ex);
}
return resultList;
}
5. list可以一边删除一边遍历吗?
list.add(1);
list.add(2);
list.add(3);
list.add(4);
Iterator<Integer> iterator = list.iterator();
while (iterator.hasNext()) {
Integer integer = iterator.next();
if (integer == 2) {
iterator.remove();
}
}