MyBatis do not write 1 = 1 for multiple query conditions

When we encounter multiple query conditions, using where 1=1 can be very convenient to solve the problem. If none of the if conditions are valid, then all conditions are invalid, i.e., simply where 1=1, so where has no meaning.

Example:

<select id="queryBookInfo" parameterType="com.tjt.platform.entity.BookInfo" resultType="java.lang.Integer"> select count(*) from t_rule_BookInfo t where 1=1 <if test="title ! =null and title ! ='' "> AND title = #{title} </if> <if test="author ! =null and author ! ='' "> AND author = #{author} </if> </select>Copy the code

Is:

<select id="queryBookInfo" parameterType="com.tjt.platform.entity.BookInfo" resultType="java.lang.Integer"> select count(*) from t_rule_BookInfo t <where> <if test="title ! =null and title ! ='' "> title = #{title} </if> <if test="author ! =null and author ! ='' "> AND author = #{author} </if> </where> </select>Copy the code

The same goes for the UPDATE operation, which can be marked 1=1 instead.

Iterates entrySet() to retrieve the Map key and value

Iterating keySet() is correct when the loop only needs to get the Map’s primary key; However, when the primary key and value are needed, iterating through entrySet() is more efficient than iterating through keySet() and then iterating through get.

Example:

Counter example: HashMap<String, String> Map = new HashMap<>(); for (String key : map.keySet()){ String value = map.get(key); }Copy the code

Is:

Example: HashMap<String, String> Map = new HashMap<>(); for (Map.Entry<String,String> entry : map.entrySet()){ String key = entry.getKey(); String value = entry.getValue(); }Copy the code

Use collection.isEmpty () to detect empty

Using collection.size () to detect empty is logically fine, but using collection.isEmpty () makes the code easier to read and gives better performance; In addition, any implementation of collection.isempty () has a time complexity of O(1) and does not require multiple loops, but some implementations with collection.size () may have a time complexity of O(n).

Example:

LinkedList<Object> collection = new LinkedList<>();
if (collection.size() == 0){
 System.out.println("collection is empty.");
}
Copy the code

Is:

LinkedList<Object> collection = new LinkedList<>(); if (collection.isEmpty()){ System.out.println("collection is empty."); IsEmpty () if (collectionUtils.isempty (collection)){system.out.println ("collection is null."); }Copy the code

When initializing a collection, try to specify its size

If possible, specify the size of the collection during initialization, which can effectively reduce the number of capacity expansion of the collection, because the time complexity of each capacity expansion of the collection is likely to be O(N), which costs time and performance.

Example:

Int [] arr = new int[]{1,2,3,4}; List<Integer> list = new ArrayList<>(); for (int i : arr){ list.add(i); }Copy the code

Is:

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

Use StringBuilder to concatenate strings

Normal string concatenation is optimized by Java at compile time, but concatenation of strings in loops cannot be optimized at compile time, so StringBuilder is used instead.

Example:

// To concatenate a String in a loop: String STR = ""; for (int i = 0; i < 10; I ++){// String concatenation in a loop is not optimized by Java. }Copy the code

Is:

String str1 = "Love"; String str2 = "Courage"; String strConcat = str1 + str2; StringBuilder sb = new StringBuilder(); for (int i = 0; i < 10; I ++){// In the loop, the Java compiler cannot optimize, so use StringBuilder sb. Append (I) manually; }Copy the code

6. Set is used if the collection. contains method is frequently called

In the Java set class library, the general time complexity of the contains method of List is O(n). If the code needs to call the contains method frequently to search for data, the set List will be converted into a HashSet implementation first, and the time complexity of O(n) will be O(1).

Example:

List<Object> List = new ArrayList<>(); for (int i = 0; i <= Integer.MAX_VALUE; I ++){// Time complexity is O(n) if (list. Contains (I)) system.out.println ("list contains "+ I); }Copy the code

Is:

List<Object> List = new ArrayList<>(); Set<Object> set = new HashSet<>(); for (int i = 0; i <= Integer.MAX_VALUE; If (set.contains(I)){system.out.println ("list contains "+ I); }}Copy the code

Use static code blocks to assign static member variables

Static code block assignments should be used for static member variables of collection types, rather than collection implementations.

Example:

Private static Map<String, Integer> Map = new HashMap<String, Integer>(){{map.put("Leo",1); private static Map<String, Integer> Map = new HashMap<String, Integer>(){{map.put("Leo",1); map.put("Family-loving",2); map.put("Cold on the out side passionate on the inside",3); }}; private static List<String> list = new ArrayList<>(){ { list.add("Sagittarius"); list.add("Charming"); list.add("Perfectionist"); }};Copy the code

Is:

Private static Map<String, Integer> Map = new HashMap<String, Integer>(); static { map.put("Leo",1); map.put("Family-loving",2); map.put("Cold on the out side passionate on the inside",3); } private static List<String> list = new ArrayList<>(); static { list.add("Sagittarius"); list.add("Charming"); list.add("Perfectionist"); }Copy the code

Delete unused local variables, method parameters, private methods, fields, and extra parentheses.

Mask constructors in utility classes

A utility class is a collection of static fields and functions that should not be instantiated; However, Java adds an implicit public constructor for every class that does not explicitly define a constructor. To avoid unnecessary instantiation, you should explicitly define a private constructor to mask this implicit public constructor.

Example:

Public Class PasswordUtils {// Negative example of the utility constructor private static final Logger LOG = LoggerFactory.getLogger(PasswordUtils.  public static final String DEFAULT_CRYPT_ALGO = "PBEWithMD5AndDES"; public static String encryptPassword(String aPassword) throws IOException { return new PasswordUtils(aPassword).encrypt(); }Copy the code

Is:

Public Class PasswordUtils {// Public static final Logger LOG = LoggerFactory.getLogger(Passwordutils.class);  // Define a private constructor to mask the implicit public constructor private PasswordUtils(){} public static final String DEFAULT_CRYPT_ALGO = "PBEWithMD5AndDES"; public static String encryptPassword(String aPassword) throws IOException { return new PasswordUtils(aPassword).encrypt(); }Copy the code

Remove redundant exception catches and throw them

After catching an exception with a catch statement, if nothing is done and the exception is simply thrown again, the effect is the same as if the exception were not caught. You can remove this piece of code or add other handling.

Example:

Private static String fileReader(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; }}Copy the code

Is:

Private static String fileReader(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) {return "fileReader Exception "; * /}}}Copy the code

ValueOf (value) instead of “” + value

When converting other objects or types to strings, it is more efficient to use string.valueof (value) rather than “”+value.

Example:

// To convert other objects or types to strings: int num = 520; // "" + value String strLove = "" + num;Copy the code

Is:

// Convert other objects or types to strings: int num = 520; String strLove = string.valueof (num); String strLove = string.valueof (num);Copy the code

Avoid Using BigDecimal(double)

BigDecimal(Double) carries the risk of loss of precision and can cause business logic exceptions in scenarios where exact calculations or value comparisons are performed.

Example:

// BigDecimal BigDecimal = new BigDecimal(0.11d);Copy the code

Is:

BigDecimal bigDecimal1 = Bigdecimal.valueof (0.11d); // BigDecimal bigDecimal1 = Bigdecimal.valueof (0.11d);Copy the code

Return empty arrays and collections instead of NULL

If the program returns NULL, the caller must enforce null detection, otherwise a null-pointer exception will be thrown. Return an empty array or collection, effectively preventing the caller from throwing a null-pointer exception because null is not detected, and making the code cleaner by removing null-detecting statements.

Example:

Public static Result[] getResults() {return null; } public static List<Result> getResultList() { return null; } public static Map<String, Result> getResultMap() { return null; }Copy the code

Is:

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(); }Copy the code

Call equals with constants or determinations in preference

The equals method of an object is prone to null-pointer exceptions and should be called using constants or objects that are determined to have values.

Example:

Private static Boolean fileReader(String fileName)throws IOException{// Return if a null pointer is thrown fileName.equals("Charming"); }Copy the code

Is:

Private static Boolean fileReader(String fileName)throws IOException{// Call the equals method using constants or objects with certain values return "Charming".equals(fileName); // Return objects.equals ("Charming",fileName) with: java.util.objects.equals (); }Copy the code

Enumeration property fields must be private and immutable

Enumerations are commonly used as constants, and their properties can be easily modified if there are public property fields or set field methods in the enumeration. Ideally, property fields in enumerations are private and assigned in private constructors, with no corresponding Setter methods, preferably with a final modifier.

Example:

Public enum SwitchStatus {// Enumeration property field example DISABLED(0, "disable "), ENABLED(1," enable "); public int value; private String description; private SwitchStatus(int value, String description) { this.value = value; this.description = description; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; }}Copy the code

Is:

Public enum SwitchStatus {// Enumeration property field examples DISABLED(0, "Disable "), ENABLED(1," enable "); // final private final int value; private final String description; private SwitchStatus(int value, String description) { this.value = value; this.description = description; } public int getValue() {return value; } public String getDescription() { return description; }}Copy the code

Split (String regex) some keywords need to be translated

When using a String String plit method, the incoming delimited String is a regular expression, are part of the keyword (such as. | etc) need to be escaped.

Example:

// string.split (String regex) example String[] split = "a.a.a.bc ".split("."); System.out.println(Arrays.toString(split)); / / results for String [] [] split1 = "a | ab | ABC." the split (" | "); System.out.println(Arrays.toString(split1)); / / the results for "a", "|", "a", "b", "|", "a", "b", "c"]Copy the code

Is:

// String. Split (String regex) String[] split2 = "a.a.b.a.bc ". Split ("\\."); System.out.println(Arrays.toString(split2)); / / results for [" a ", "ab", "ABC"] / / | need to translate the String [] split3 = "a | ab | ABC." the split (" \ \ | "); System.out.println(Arrays.toString(split3)); // Result is ["a", "ab", "ABC "]Copy the code