(1) object.equals("test");
object容易报空指针异常,推荐使用 "test".equals(object);
JDK7 引入了 java.util.Objects#equals ,Objects.equals(o1,o2)已经处理了空指针。
(2) 遍历Map时候有三种方式:
1>keySet 其实keySet遍历了两次,一次是转为iterator对象,第二次是从hashMap取出相应的key对应的value
2>entrySet 遍历一次把key-value都放入了entry中
3>JDK8提供的Map.foreach方法。推荐使用
(3) 创建线程资源必须通过线程池来提供,不允许自行显式创建线程
(4) SimpleDateFormat是线程非安全类,不要定义为static。可以使用DateUtils工具类,也可使用以下方式:
private static final ThreadLocal<DateFormat> df = new ThreadLocal<DateFormat>() {
@Override
protected DateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd");
}
};
JDK8中提供了更好的方式:Instant代替Date,LocalDateTime代替Calendar,DateTimeFormatter代替SImpleDateFormatter。
private void timeDD() {
Instant now = Instant.now();
LocalDateTime localDateTime=LocalDateTime.now();
DateTimeFormatter dateTimeFormatter= DateTimeFormatter.ofPattern("yyyy-MM-dd");
String nowFormat=dateTimeFormatter.format(now);
}
(5) Random被多线程使用。会因竞争同一个seed导致性能下降
private void randomDD(){
/*java.util.Random实例*/
Double randomDouble1=new java.util.Random().nextDouble();
/*Math.random实例*/
Double randomDouble2=Math.random();
/*JDK7之后可以直接使用 API ThreadLocalRandom ,推荐使用*/
Double randomDouble3= ThreadLocalRandom.current().nextDouble();
}