参考: https://blog.csdn.net/csdnnews/article/details/100987866 阿里java开发规范
需要 Map 的主键和取值时,应该迭代 entrySet() 当循环中只需要 Map 的主键时,迭代 keySet() 是正确的。但是,当需要主键和取值时,迭代 entrySet() 才是更高效的做法,比先迭代 keySet() 后再去 get 取值性能更佳。
反例:
Map<String, String> map = ...; for (String key : map.keySet()) { String value = map.get(key); ... }正例:
Map<String, String> map = ...; for (Map.Entry<String, String> entry : map.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); ... }应该使用Collection.isEmpty()检测空
使用 Collection.size() 来检测空逻辑上没有问题,但是使用 Collection.isEmpty()使得代码更易读,并且可以获得更好的性能。任何 Collection.isEmpty() 实现的时间复杂度都是 O(1) ,但是某些 Collection.size() 实现的时间复杂度可能是 O(n) 。
反例:
if (collection.size() == 0) { ... }正例:
if (collection.isEmpty()) { ... }如果需要还需要检测 null ,可采用CollectionUtils.isEmpty(collection)和CollectionUtils.isNotEmpty(collection)。
不要把集合对象传给自己 此外,由于某些方法要求参数在执行期间保持不变,因此将集合传递给自身可能会导致异常行为。
反例:
List<String> list = new ArrayList<>(); list.add("Hello"); list.add("World"); if (list.containsAll(list)) { // 无意义,总是返回true ... }list.removeAll(list); // 性能差, 直接使用clear()
集合初始化尽量指定大小 Java 的集合类用起来十分方便,但是看源码可知,集合也是有大小限制的。每次扩容的时间复杂度很有可能是 O(n) ,所以尽量指定可预知的集合大小,能减少集合的扩容次数。
反例:
int[] arr = new int[]{1, 2, 3}; List<Integer> list = new ArrayList<>(); for (int i : arr) { list.add(i); }正例:
int[] arr = new int[]{1, 2, 3}; List<Integer> list = new ArrayList<>(arr.length); for (int i : arr) { list.add(i); }字符串拼接使用 StringBuilder
一般的字符串拼接在编译期 Java 会进行优化,但是在循环中字符串拼接, Java 编译期无法做到优化,所以需要使用 StringBuilder 进行替换。
反例:
String s = ""; for (int i = 0; i < 10; i++) { s += i; }正例:
String a = "a"; String b = "b"; String c = "c"; String s = a + b + c; // 没问题,java编译器会进行优化 StringBuilder sb = new StringBuilder(); for (int i = 0; i < 10; i++) { sb.append(i); // 循环中,java编译器无法进行优化,所以要手动使用StringBuilder }List 的随机访问 大家都知道数组和链表的区别:数组的随机访问效率更高。当调用方法获取到 List 后,如果想随机访问其中的数据,并不知道该数组内部实现是链表还是数组,怎么办呢?可以判断它是否实现* RandomAccess *接口。
正例:
// 调用别人的服务获取到list
List<Integer> list = otherService.getList(); if (list instanceof RandomAccess) { // 内部数组实现,可以随机访问 System.out.println(list.get(list.size() - 1)); } else { // 内部可能是链表实现,随机访问效率低 }频繁调用 Collection.contains 方法请使用 Set
在 Java 集合类库中,List 的 contains 方法普遍时间复杂度是 O(n) ,如果在代码中需要频繁调用 contains 方法查找数据,可以先将 list 转换成 HashSet 实现,将 O(n) 的时间复杂度降为 O(1) 。
反例:
ArrayList<Integer> list = otherService.getList(); for (int i = 0; i <= Integer.MAX_VALUE; i++) { // 时间复杂度O(n) list.contains(i); }正例:
ArrayList<Integer> list = otherService.getList(); Set<Integer> set = new HashSet(list); for (int i = 0; i <= Integer.MAX_VALUE; i++) { // 时间复杂度O(1) set.contains(i); }BigDecimal(double) 存在精度损失风险,在精确计算或值比较的场景中可能会导致业务逻辑异常。
反例:
BigDecimal value = new BigDecimal(0.1D); // 0.100000000000000005551115...正例:
BigDecimal value = BigDecimal.valueOf(0.1D);; // 0.1返回空数组和空集合而不是 null 返回 null ,需要调用方强制检测 null ,否则就会抛出空指针异常。返回空数组或空集合,有效地避免了调用方因为未检测 null 而抛出空指针异常,还可以删除调用方检测 null 的语句使代码更简洁。
反例:
public static Result[] getResults() { return null; } public static List<Result> getResultList() { return null; } public static Map<String, Result> getResultMap() { return null; } public static void main(String[] args) { Result[] results = getResults(); if (results != null) { for (Result result : results) { ... } } List<Result> resultList = getResultList(); if (resultList != null) { for (Result result : resultList) { ... } } Map<String, Result> resultMap = getResultMap(); if (resultMap != null) { for (Map.Entry<String, Result> resultEntry : resultMap) { ... } } }正例:
public static Result[] getResults() { return new Result[0]; } public static List<Result> getResultList() { return Collections.emptyList(); } public static Map<String, Result> getResultMap() { return Collections.emptyMap(); } public static void main(String[] args) { Result[] results = getResults(); for (Result result : results) { ... } List<Result> resultList = getResultList(); for (Result result : resultList) { ... } Map<String, Result> resultMap = getResultMap(); for (Map.Entry<String, Result> resultEntry : resultMap) { ... } }