java代码性能优化,提高健壮性

我们在后端开发中,经常会遇到各种“坑”,比如大家最熟悉的空指针异常、session失效等等,其实很大原因在于开发过程中并没有很好的将用法与实际相结合,如果我们能注意一些代码规范和技巧,可以使我们的开发事半功倍!我将从以下三个方面分享我在开发过程中进行的优化。

让代码性能更高 让代码更优雅 让代码远离 bug

让代码性能更高

1..需要 Map 的主键和取值时,应该迭代 entrySet()

当循环中只需要 Map 的主键时,迭代 keySet() 是正确的。但是,当需要主键和取值时,迭代 entrySet() 才是更高效的做法,比先迭代 keySet() 后再去 get 取值性能更佳。

反例

<code>Map<string> map = ...;
for(String key: map.keySet()) {
\tString value = map.get(key);
\t...
}/<string>/<code>

正例

<code>Map<string> map = ...;
for (Map.Entry<string> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
...
}/<string>/<string>/<code>

2.应该使用Collection.isEmpty()检测空

使用 Collection.size() 来检测空逻辑上没有问题,但是使用 Collection.isEmpty()使得代码更易读,并且可以获得更好的性能。任何 Collection.isEmpty() 实现的时间复杂度都是 O(1) ,但是某些 Collection.size() 实现的时间复杂度可能是

O(n) 。

反例

<code>if (collection.size() == 0) {
...
}/<code>

正例

<code>if (collection.isEmpty()) {
...
}/<code>

3.集合初始化尽量指定大小

java 的集合类用起来十分方便,但是看源码可知,集合也是有大小限制的。每次扩容的时间复杂度很有可能是 O(n) ,所以尽量指定可预知的集合大小,能减少集合的扩容次数。

反例

<code>int[] arr = new int[]{1, 2, 3};
List<integer> list = new ArrayList<>();
for (int i : arr) {
list.add(i);
}/<integer>/<code>

正例

<code>int[] arr = new int[]{1, 2, 3};
List<integer> list = new ArrayList<>(arr.length);
for (int i : arr) {
list.add(i);

}/<integer>/<code>

4.字符串拼接使用 StringBuilder

一般的字符串拼接在编译期 java 会进行优化,但是在循环中字符串拼接, java 编译期无法做到优化,所以需要使用 StringBuilder 进行替换。 反例

<code>String s = "";
for (int i = 0; i < 10; i++) {
s += i;
}/<code>

正例

<code>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
}/<code>

5.List 的随机访问

数组和链表的区别:数组的随机访问效率更高。当调用方法获取到 List 后,如果想随机访问其中的数据,并不知道该数组内部实现是链表还是数组,怎么办呢?可以判断它是否实现 RandomAccess 接口。

正例

<code>// 调用服务获取到list
List<integer> list = otherService.getList();
if (list instanceof RandomAccess) {
// 内部数组实现,可以随机访问
System.out.println(list.get(list.size() - 1));
} else {
// 内部可能是链表实现,随机访问效率低
}/<integer>/<code>

6.频繁调用 Collection.contains 方法请使用 Set

在 java 集合类库中,List 的 contains 方法普遍时间复杂度是 O(n) ,如果在代码中需要频繁调用 contains 方法查找数据,可以先将 list 转换成 HashSet 实现,将 O(n) 的时间复杂度降为 O(1) 。

反例

<code>ArrayList<integer> list = otherService.getList();
for (int i = 0; i <= Integer.MAX_VALUE; i++) {
// 时间复杂度O(n)
list.contains(i);
}/<integer>/<code>

正例

<code>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);
}/<integer>/<integer>/<code>

让代码更优雅

1.长整型常量后添加大写 L

反例

<code>long value = 1l;
long max = Math.max(1L, 5);/<code>

正例

<code>long value = 1L;
long max = Math.max(1L, 5L);/<code>

2.不要使用魔法值

当你编写一段代码时,使用魔法值可能看起来很明确,但在调试时它们却不显得那么明确了。这就是为什么需要把魔法值定义为可读取常量的原因。但是,-1、0 和 1不被视为魔法值。

反例

<code>for (int i = 0; i < 100; i++){
...
}
if (a == 100) {
...
}/<code>

正例

<code>private static final int MAX_COUNT = 100;
for (int i = 0; i < MAX_COUNT; i++){
...
}
if (count == MAX_COUNT) {

...
}/<code>

3.不要使用集合实现来赋值静态成员变量

对于集合类型的静态成员变量,不要使用集合实现来赋值,应该使用静态代码块赋值。

反例

<code>private static Map<string> map = new HashMap<string>() {
{
put("a", 1);
put("b", 2);
}
};


private static List<string> list = new ArrayList<string>() {
{
add("a");
add("b");
}
};/<string>/<string>/<string>/<string>/<code>

正例

<code>private static Map<string> map = new HashMap<>();
static {
map.put("a", 1);
map.put("b", 2);
};

private static List<string> list = new ArrayList<>();
static {
list.add("a");
list.add("b");
};/<string>/<string>/<code>

4.删除未使用的私有方法和字段

删除未使用的私有方法和字段,使代码更简洁更易维护。若有需要再使用,可以从历史提交中找回。

反例

<code>public class DoubleDemo1 {
private int unusedField = 100;
private void unusedMethod() {
...
}
public int sum(int a, int b) {
return a + b;
}
}/<code>

正例

<code>public class DoubleDemo1 {
public int sum(int a, int b) {
return a + b;
}
}/<code>

5.工具类应该屏蔽构造函数

工具类是一堆静态字段和函数的集合,不应该被实例化。但是,Java 为每个没有明确定义构造函数的类添加了一个隐式公有构造函数。所以,为了避免 java "小白"使用有误,应该显式定义私有构造函数来屏蔽这个隐式公有构造函数。 反例

<code>public class MathUtils {
public static final double PI = 3.1415926D;

public static int sum(int a, int b) {
return a + b;
}
}/<code>

正例

<code>public class MathUtils {
public static final double PI = 3.1415926D;
private MathUtils() {}
public static int sum(int a, int b) {
return a + b;
}
}/<code>

6.公有静态常量应该通过类访问

虽然通过类的实例访问公有静态常量是允许的,但是容易让人它误认为每个类的实例都有一个公有静态常量。所以,公有静态常量应该直接通过类访问。

反例

<code>public class User {
public static final String CONST_NAME = "name";
...
}
User user = new User();
String nameKey = user.CONST_NAME;/<code>

正例

<code>public class User {
public static final String CONST_NAME = "name";
...
}
String nameKey = User.CONST_NAME;/<code>

7.不要用NullPointerException判断空

空指针异常应该用代码规避(比如检测不为空),而不是用捕获异常的方式处理。

反例

<code>public String getUserName(User user) {
try {
return user.getName();
} catch (NullPointerException e) {
return null;
}
}/<code>

正例

<code>public String getUserName(User user) {
if (Objects.isNull(user)) {
return null;
}
return user.getName();
}
/<code>

8.使用String.valueOf(value)代替""+value

当要把其它对象或类型转化为字符串时,使用 String.valueOf(value) 比""+value 的效率更高。

反例

<code>int i = 1;
String s = "" + i;
/<code>

正例

<code>int i = 1;
String s = String.valueOf(i);/<code>

9.过时代码添加 @Deprecated 注解

当一段代码过时,但为了兼容又无法直接删除,不希望以后有人再使用它时,可以添加 @Deprecated 注解进行标记。在文档注释中添加 @deprecated 来进行解释,并提供可替代方案。

正例

<code>/**
* 保存
*
* @deprecated 此方法效率较低,请使用{@link newSave()}方法替换它
*/
@Deprecated
public void save(){
// do something
}/<code>

让代码远离 bug

1.禁止使用构造方法 BigDecimal(double)

BigDecimal(double) 存在精度损失风险,在精确计算或值比较的场景中可能会导致业务逻辑异常。

反例

<code>BigDecimal value = new BigDecimal(0.1D); // 0.100000000000000005551115.../<code>

正例

<code>BigDecimal value = BigDecimal.valueOf(0.1D);; // 0.1/<code>

2.返回空数组和空集合而不是 null

返回 null ,需要调用方强制检测 null ,否则就会抛出空指针异常。返回空数组或空集合,有效地避免了调用方因为未检测 null 而抛出空指针异常,还可以删除调用方检测 null 的语句使代码更简洁。

反例

<code>
public static Result[] getResults() {
return null;
}


public static List<result> getResultList() {
return null;
}


public static Map<string> 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> resultMap = getResultMap();
if (resultMap != null) {
for (Map.Entry<string> resultEntry : resultMap) {
...
}
}
}/<string>/<string>/<result>/<string>/<result>/<code>

正例

<code>public static Result[] getResults() {
return new Result[0];
}


public static List<result> getResultList() {
return Collections.emptyList();
}


public static Map<string> 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> resultMap = getResultMap();
for (Map.Entry<string> resultEntry : resultMap) {
...
}
}

/<string>/<string>/<result>/<string>/<result>/<code>

3.优先使用常量或确定值来调用 equals 方法

对象的 equals 方法容易抛空指针异常,应使用常量或确定有值的对象来调用 equals 方法。当然,使用 java.util.Objects.equals() 方法是最佳实践。

反例

<code>public void isFinished(OrderStatus status) {
return status.equals(OrderStatus.FINISHED); // 可能抛空指针异常
}/<code>

正例

<code>public void isFinished(OrderStatus status) {
return OrderStatus.FINISHED.equals(status);
}


public void isFinished(OrderStatus status) {
return Objects.equals(status, OrderStatus.FINISHED);
}/<code>

4.枚举的属性字段必须是私有不可变

枚举通常被当做常量使用,如果枚举中存在公共属性字段或设置字段方法,那么这些枚举常量的属性很容易被修改。理想情况下,枚举中的属性字段是私有的,并在私有构造函数中赋值,没有对应的 Setter 方法,最好加上final修饰符

反例

<code>public enum UserStatus {
DISABLED(0, "禁用"),
ENABLED(1, "启用");


public int value;
private String description;


private UserStatus(int value, String description) {
this.value = value;
this.description = description;
}


public String getDescription() {
return description;
}


public void setDescription(String description) {
this.description = description;
}
}/<code>

正例

<code>public enum UserStatus {
DISABLED(0, "禁用"),
ENABLED(1, "启用");


private final int value;
private final String description;


private UserStatus(int value, String description) {
this.value = value;
this.description = description;
}


public int getValue() {
return value;
}


public String getDescription() {
return description;
}
}/<code>

5.小心String.split(String regex)

字符串 String 的 split 方法,传入的分隔字符串是正则表达式!部分关键字(比如.| 等)需要转义。

反例

<code>"a.ab.abc".split("."); // 结果为[]
"a|ab|abc".split("|"); // 结果为["a", "|", "a", "b", "|", "a", "b", "c"]/<code>

正例

<code>"a.ab.abc".split("\\\\."); // 结果为["a", "ab", "abc"]
"a|ab|abc".split("\\\\|"); // 结果为["a", "ab", "abc"]/<code>


作者:java酱
链接:https://juejin.im/post/5e0ca85e6fb9a047ec702688
来源:掘金

来源:https://segmentfault.com/a/1190000022029639喜欢对你有帮助的话记得加个关注不迷路哦

还有关注我私信回复【资料】可以领取到一些个人收集的面试及电子书资料,或许对你有帮助!

java代码性能优化,提高健壮性


java代码性能优化,提高健壮性


分享到:


相關文章: