1. 当成员变量值无需改变时,尽量定义为静态常量
public class HttpConnection{ private final long timeout = 5L; }
public class HttpConnection{ private static final long TIMEOUT = 5L; }
2. 尽量使用基本数据类型,避免自动装箱拆箱和空指针的判断
public static Integer isValid() { Integer sum = 0; int[] values = ...; for (int value : values) { sum += value; } return sum; }
public static int isValid() { int sum = 0; int[] values = ...; for (int value : values) { sum += value; } return sum; }
3. 尽量使用函数内的基本类型临时变量
public final class Accumulator { private double result = 0.0D; public void addAll(@NotNull double[] values) { for (double value : values) { result += value; } } }
public final class Accumulator { private double result = 0.0D; public void addAll(@NotNull double[] values) { double sum = 0.0D; for (double value : values) { sum += value; } result += sum; } }
4. 尽量不使用反射赋值对象
List<UserDO> userDOList = ...; List<UserVO> userVOList = new ArrayList<>(userDOList.size()); for (UserDO userDO : userDOList) { UserVO userVO = new UserVO(); BeanUtils.copyProperties(userDO, userVO); userVOList.add(userVO); }
List<UserDO> userDOList = ...; List<UserVO> userVOList = new ArrayList<>(userDOList.size()); for (UserDO userDO : userDOList) { userVOList.add(new UserVO( userDO.getName(), userDO.getPhone(), userDO.getIcon() )); }
5. 把跟类成员变量无关的方法声明成静态方法
public int getMonth(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.MONTH) + 1; }
public static int getMonth(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.MONTH) + 1; }
6. 协议方法参数值非空,避免不必要的空指针判断
public static boolean isValid(UserDO user) { if (Objects.isNull(user)) { return false; } return Boolean.TRUE.equals(user.getIsValid()); }
public static boolean isValid(@NotNull UserDO user) { return Boolean.TRUE.equals(user.getIsValid()); }
7. 尽量减少方法的重复调用
List<UserDO> userList = ...; for (int i = 0; i < userList.size(); i++) { ... }
List<UserDO> userList = ...; int userLength = userList.size(); for (int i = 0; i < userLength; i++) { ... }
8. 尽量避免不必要的方法调用
List<UserDO> userList = userDAO.queryActive(); if (isAll) { userList = userDAO.queryAll(); }
List<UserDO> userList; if (isAll) { userList = userDAO.queryAll(); }else{ userList = userDAO.queryActive(); }
9. 尽量使用位移来代替正整数乘除
int num1 = a * 4; int num2 = a / 4;
int num1 = a << 2; int num2 = a >> 2;
10. 对于多常量选择分支,尽量使用switch语句而不是if-else语句
11. 不要使用””+转化字符串
int i = 12345; String s = "" + i;
int i = 12345; String s = String.valueOf(i);
12. 尽量使用Arrays.asList转化数组为列表
public void demo() { List<String> typeList = new ArrayList<>(8); typeList.add("Short"); typeList.add("Long"); typeList.add("Integer"); String[] names = ...; List<String> nameList = ...; for (Sting name : names) { nameList.add(name); } }
public void demo() { List<String> typeList = new ArrayList<>(8); typeList.add("Short"); typeList.add("Long"); typeList.add("Integer"); String[] names = ...; List<String> nameList = ...; nameList.addAll(Arrays.asList(names)); }
13. 直接迭代需要使用的集合
public void demo() { Map<Long, UserDO> userMap = ...; for (Long userId : userMap.keySet()) { UserDO user = userMap.get(userId); } }
public void demo() { Map<Long, UserDO> userMap = ...; for (Map.Entry<Long, UserDO> userEntry : userMap.entrySet()) { Long userId = userEntry.getKey(); UserDO user = userEntry.getValue(); } }
14. 不要使用size方法检测空,必须使用isEmpty方法测空
public void demo() { List<UserDO> userList = ...; if (userList.size() == 0) { ... } Map<Long, UserDO> userMap = ...; if (userMap.size() == 0) { ... } }
public void demo() { List<UserDO> userList = ...; if (userList.isEmpty()) { ... } Map<Long, UserDO> userMap = ...; if (userMap.isEmpty()) { ... } }
15. 非随机访问的List,尽量使用迭代代替随机访问
public void demo() { LinkedList<UserDO> userDOLinkedList = ...; int size = userDOLinkedList.size(); for (int i = 0; i < size; i++) { UserDO userDO = userDOLinkedList.get(i); } }
public void demo() { LinkedList<UserDO> userDOLinkedList = ...; int size = userDOLinkedList.size(); for (UserDO userDO : userDOLinkedList) { ... } }
16. 尽量使用HashSet判断值存在
public void demo() { List<Long> adminList = ...; List<UserDO> userDOList = ...; List<UserVO> userVOList = new ArrayList<>(userDOList.size()); for (UserDO userDO : userDOList) { if (adminList.contains(userDO.getId())) { userVOList.add(transUser(userDO)); } } }
public void demo() { Set<Long> adminList = ...; List<UserDO> userDOList = ...; List<UserVO> userVOList = new ArrayList<>(userDOList.size()); for (UserDO userDO : userDOList) { if (adminList.contains(userDO.getId())) { userVOList.add(transUser(userDO)); } } }
17. 避免先判断存在再进行获取
public static UserVO transUser(UserDO user, Map<Long, RoleDO> roleMap) { UserVO userVO = new UserVO(); userVO.setId(user.getId()); if (roleMap.containsKey(user.getRoleId)) { userVO.setRole(transRole(role)); } }
public static UserVO transUser(UserDO user, Map<Long, RoleDO> roleMap) { UserVO userVO = new UserVO(); userVO.setId(user.getId()); RoleDO role = roleMap.get(user.getRoleId()); if (Objects.nonNull(role)) { userVO.setRole(transRole(role)); } }
18. 直接捕获对应的异常
public void demo() { try { saveData(); } catch (Exception e) { if (e instanceof IOException) { log.error("保存数据IO异常", e); } else { log.error("保存数据其他异常", e); } } }
public void demo() { try { saveData(); } catch (IOException e) { log.error("保存数据IO异常", e); } catch (Exception e) { log.error("保存数据其他异常", e); } }
19. 尽量使用线程池减少线程开销
21. 工具类中屏蔽构造函数
22. 返回空数组和集合而非null
public static List<Result> getResultList() { return null; }
public static List<Result> getResultList() { return Collections.EMPTY_LIST; }
23. 优先使用常量或者确定值调用equals方法
private static boolean fileReader(String fileName) { // 可能报空异常 return fileName.equals("Java开发手册"); }
private static boolean fileReader(String fileName) { return "Java开发手册".equals(fileName); }
24. 使用通用工具函数Objects.equals()
thisName != null && thisName.equals(name)
thisName == name || (thisName != null && thisName.equals(name))
Objects.equals(name, thisName)
25. 使用通用工具函数CollectionUtil.isEmpty()
list != null && list.isEmpty()
CollectionUtils.isNotEmpty(list)
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算