bad.java
Map<String, String> map = ...;
for (String key : map.keySet()) {
String value = map.get(key);
...
}
good.java
Map<String, String> map = ...;
for (Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
...
}
bad.java
if (collection.size() == 0) {
...
}
good.java
if (collection.isEmpty()) {
...
}
bad.java
List<String> list = new ArrayList<>();
list.add("Hello");
list.add("World");
if (list.containsAll(list)) { //It doesn't make sense, it's true.
...
}
list.removeAll(list); //Poor performance, clear()use.
bad.java
int[] arr = new int[]{1, 2, 3};
List<Integer> list = new ArrayList<>();
for (int i : arr) {
list.add(i);
}
good.java
int[] arr = new int[]{1, 2, 3};
List<Integer> list = new ArrayList<>(arr.length);
for (int i : arr) {
list.add(i);
}
bad.java
String s = "";
for (int i = 0; i < 10; i++) {
s += i;
}
good.java
String a = "a";
String b = "b";
String c = "c";
String s = a + b + c; //No problem, java compiler can be optimized
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10; i++) {
sb.append(i); //Use manual StringBuilder as the java compiler cannot be optimized inside the loop
}
good.java
List<Integer> list = otherService.getList();
if (list instanceof RandomAccess) {
//Internally it is realized by an array, so it can be randomized
System.out.println(list.get(list.size() - 1));
} else {
//Poor performance that may be achieved internally with linked lists
}
bad.java
ArrayList<Integer> list = otherService.getList();
for (int i = 0; i <= Integer.MAX_VALUE; i++) {
//Time complexity O(n)
list.contains(i);
}
good.java
ArrayList<Integer> list = otherService.getList();
Set<Integer> set = new HashSet(list);
for (int i = 0; i <= Integer.MAX_VALUE; i++) {
//Time complexity O(1)
set.contains(i);
}
bad.java
long value = 1l;
long max = Math.max(1L, 5);
good.java
long value = 1L;
long max = Math.max(1L, 5L);
bad.java
for (int i = 0; i < 100; i++){
...
}
if (a == 100) {
...
}
good.java
private static final int MAX_COUNT = 100;
for (int i = 0; i < MAX_COUNT; i++){
...
}
if (count == MAX_COUNT) {
...
}
bad.java
private static Map<String, Integer> map = new HashMap<String, Integer>() {
{
put("a", 1);
put("b", 2);
}
};
private static List<String> list = new ArrayList<String>() {
{
add("a");
add("b");
}
};
good.java
private static Map<String, Integer> 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");
};
bad.java
private void handle(String fileName) {
BufferedReader reader = null;
try {
String line;
reader = new BufferedReader(new FileReader(fileName));
while ((line = reader.readLine()) != null) {
...
}
} catch (Exception e) {
...
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
...
}
}
}
}
good.java
private void handle(String fileName) {
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = reader.readLine()) != null) {
...
}
} catch (Exception e) {
...
}
}
bad.java
public class DoubleDemo1 {
private int unusedField = 100;
private void unusedMethod() {
...
}
public int sum(int a, int b) {
return a + b;
}
}
good.java
public class DoubleDemo1 {
public int sum(int a, int b) {
return a + b;
}
}
bad.java
public int sum(int a, int b) {
int c = 100;
return a + b;
}
good.java
public int sum(int a, int b) {
return a + b;
}
bad.java
public int sum(int a, int b, int c) {
return a + b;
}
good.java
public int sum(int a, int b) {
return a + b;
}
bad.java
return (x);
return (x + 2);
int x = (y * 3) + 1;
int m = (n * 4 + 2);
good.java
return x;
return x + 2;
int x = y * 3 + 1;
int m = n * 4 + 2;
bad.java
public class MathUtils {
public static final double PI = 3.1415926D;
public static int sum(int a, int b) {
return a + b;
}
}
good.java
public class MathUtils {
public static final double PI = 3.1415926D;
private MathUtils() {}
public static int sum(int a, int b) {
return a + b;
}
}
bad.java
private static String readFile(String fileName) throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
String line;
StringBuilder builder = new StringBuilder();
while ((line = reader.readLine()) != null) {
builder.append(line);
}
return builder.toString();
} catch (Exception e) {
throw e;
}
}
good.java
private static String readFile(String fileName) throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
String line;
StringBuilder builder = new StringBuilder();
while ((line = reader.readLine()) != null) {
builder.append(line);
}
return builder.toString();
}
}
bad.java
public class User {
public static final String CONST_NAME = "name";
...
}
User user = new User();
String nameKey = user.CONST_NAME;
good.java
public class User {
public static final String CONST_NAME = "name";
...
}
String nameKey = User.CONST_NAME;
bad.java
public String getUserName(User user) {
try {
return user.getName();
} catch (NullPointerException e) {
return null;
}
}
good.java
public String getUserName(User user) {
if (Objects.isNull(user)) {
return null;
}
return user.getName();
}
bad.java
int i = 1;
String s = "" + i;
good.java
int i = 1;
String s = String.valueOf(i);
good.java
/**
*Save
*
* @deprecated Because this method is inefficient@link newSave()Replace using the method.
*/
@Deprecated
public void save(){
// do something
}
bad.java
BigDecimal value = new BigDecimal(0.1D); // 0.100000000000000005551115...
good.java
BigDecimal value = BigDecimal.valueOf(0.1D);; // 0.1
bad.java
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) {
...
}
}
}
good.java
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) {
...
}
}
bad.java
public void isFinished(OrderStatus status) {
return status.equals(OrderStatus.FINISHED); //May be Null PointerException
}
good.java
public void isFinished(OrderStatus status) {
return OrderStatus.FINISHED.equals(status);
}
public void isFinished(OrderStatus status) {
return Objects.equals(status, OrderStatus.FINISHED);
}
bad.java
public enum UserStatus {
DISABLED(0, "Invalid"),
ENABLED(1, "Effectiveness");
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;
}
}
good.java
public enum UserStatus {
DISABLED(0, "Invalid"),
ENABLED(1, "Effectiveness");
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;
}
}
bad.java
"a.ab.abc".split("."); //Result is[]
"a|ab|abc".split("|"); //Result is["a", "|", "a", "b", "|", "a", "b", "c"]
good.java
"a.ab.abc".split("\\."); //Result is["a", "ab", "abc"]
"a|ab|abc".split("\\|"); //Result is["a", "ab", "abc"]
Reference Translated from Alibaba Developer District
Recommended Posts