阿里巴巴Java开发手册1.4.0总结

1、【强制】所有的pojo类属性必须使用包装数据类型
2、【强制】所有的局部变量使用基本数据类型
3、【强制】pojo类必须写toString方法;
4、 两个对象若相同,则hashCode值必然相同;但是hashCode值相同,两个对象不一定相同;
5、【强制】对原集合元素的增加、删除操作,均会导致列表遍历发生变化;size会变动,产生ConcurrentModificationException异常;可以使用Iterator删除元素,

while (list.iterator().hasNext()) {
     if ("当list.iterator().next() 满足删除条件时") {
         list.iterator().remove();
     }
 }

6、【强制】使用集合转数组的方法,必须使用集合的toArray(T[] array),大小list.size();如果数组元素个数大于实际所需,下标大于等于list.size()的数组元素将被置为null;
7、PECS(Producer Extends Consumer Super)原则:第一、<? extends T>适合频繁读取内容。第二、<? super T>适合经常插入内容;
8、【推荐】使用entrySet遍历Map集合KV,而不是keySet方式进行遍历;但是代码中一般是使用keySet(配合map.get(key))就可以达到目的了,因为感觉entrySet出的内容好长,如果不影响效率,感觉因人而异;除此之外,Java8新特性map.foreach循环map的key/value值的代码更加简洁,map.foreach((key, value) -> { System.out.print(key + "-" + value) }),看起来就高大上;

Map<String, String> map = new HashMap<>();
map.put("aa", "11");
map.put("bb", "22");
map.put("cc", "33");
map.put("dd", "44");
// 传统的Map迭代方式
Set<Entry<String, String>> entrySet = map.entrySet();
for (Entry<String, String> entry : entrySet) {
    System.out.println(entry.getKey() + "-" + entry.getValue());
}

// JDK8的迭代方式
map.forEach((key, value) -> {
    System.out.println(key + "-" + value);
});

9、【推荐】count ++;了解一下 附加代码;

AtomicInteger count = new AtomicInteger();
count.incrementAndGet();
System.out.println(count);// 结果:1

LongAdder count2 = new LongAdder();
count2.increment();
System.out.println(count2);// 结果:1

10、【强制】switch语句的使用,建议在每个switch块内都添加default语句,即使为空代码;
11、【参考】被声明为private只会被自己调用的方法,对于已知非空参数可以跳过非空校验;
12、【推荐】TODO(代办事宜),FIXME(错误,不能工作);
13、【推荐】任何数据结果的构造或初始化,都应制定大小,避免数据结构无线增长吃光内存;
14、【强制】如果想得到一个随机数,不要用返回类型为double的Math.random()扩大十倍去取值,而且这样取到的是0~9的数字;建议直接使用Random对象的nextInt或者nextLong方法;
15、【强制】日志打印使用slf4j,打印内容使用占位符。例:logger.debug("Processing trade with id: {} and symbol : {} ", id, symbol);;

logger.debug("Processing trade with id: {} and symbol : {} ", id, symbol);

16、【MySQL数据库】表名、字段名必须使用小写字母或数字,禁止出现数字开头,禁止两个下划线中间只出现数字;
17、【强制】即使双表join也要注意表索引、SQL性能(添加索引和添加外键其实目的是一致的);
18、【推荐】针对超多数据的分页SQL,建议先定位查询的id,根据id筛选符合条件的数据;原因:MySQL并不是跳过offset行,而是取offset + N行,然后放弃前offset行,返回那剩下的N行;例:SELECT a.* FROM 表 1 a, (select id from 表 1 where 条件 LIMIT 100000,20 ) b where a.id=b.id。

SELECT a.* FROM 表 1 a, (select id from1 where 条件 LIMIT 100000,20 ) b where a.id=b.id;

猜你喜欢

转载自www.cnblogs.com/huakaiyoushi/p/11127718.html