Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write canonical code in java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "how to write standard code in java". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. MyBatis do not write 1 = 1 for multiple query conditions

When we encounter multiple query conditions, using where 1 query 1 can easily solve our problem, but this is likely to cause a very large performance loss, because after adding the filter condition of "where 1", the database system will not be able to use query optimization strategies such as index, and the database system will be forced to scan each row of data (that is, full table scan) to compare whether the row meets the filter condition. The query speed will be very slow when the amount of data in the table is large. In addition, there is a risk of SQL injection.

Counterexample:

Select count (*) from t_rule_BookInfo t where 1131 AND title = # {title} AND author = # {author}

Positive example:

Select count (*) from t_rule_BookInfo t title = # {title} AND author = # {author}

The same is true for UPDATE operations, where tags can be used instead of 1x1.

Second, iterate entrySet () to get the key and value of Map

Iterating over keySet () is correct when you only need to get Map's primary key key in the loop; however, iterating over entrySet () is more efficient when you need the primary key key and the value value, which is better than iterating keySet () first and then getting values through get.

Counterexample:

/ / Map get value counterexample: HashMap map = new HashMap (); for (String key: map.keySet ()) {String value = map.get (key);}

Positive example:

/ / Map get key & value positive example: HashMap map = new HashMap (); for (Map.Entry entry: map.entrySet ()) {String key = entry.getKey (); String value = entry.getValue ();}

Third, use Collection.isEmpty () to detect null

It is logically OK to use Collection.size () to detect whether it is empty, but using Collection.isEmpty () makes the code easier to read and achieve better performance; in addition, the time complexity of any Collection.isEmpty () implementation is O (1) and does not require multiple iterations, but some of the time complexity implemented by the Collection.size () method may be O (n)

Counterexample:

LinkedList collection = new LinkedList (); if (collection.size () = = 0) {System.out.println ("collection is empty.");}

Positive example:

LinkedList collection = new LinkedList (); if (collection.isEmpty ()) {System.out.println ("collection is empty.");} / / check whether it is null. You can use CollectionUtils.isEmpty () if (CollectionUtils.isEmpty (collection)) {System.out.println ("collection is null.");}

Fourth, try to specify the size of the collection when initializing it.

Specifying the size of the collection during initialization can effectively reduce the number of expansion of the collection, because the time complexity of each expansion is likely to be O (n), which takes time and performance.

Counterexample:

/ / initialize list and add elements to the list counterexample: int [] arr = new int [] {1 arr = 3 arr 4}; List list = new ArrayList (); for (int I: arr) {list.add (I);}

Positive example:

/ / initialize the list and add elements to the list positive example: int [] arr = new int [] {1meme 2je 3jue 4}; / / specify the capacity size of the set list List list = new ArrayList (arr.length); for (int I: arr) {list.add (I);}

Use StringBuilder to concatenate strings

General string concatenation is optimized by Java at compile time, but cannot be optimized at compile time for string stitching Java in a loop, so StringBuilder is needed to replace it.

Counterexample:

/ / string concatenation in loop counterexample String str = "; for (int I = 0; I < 10; iSplice +) {/ / string concatenation Java in loop does not optimize str + = I;}

Positive example:

/ / string concatenation in a loop positive example String str1 = "Love"; String str2 = "Courage"; String strConcat = str1 + str2; / / Java compiler optimizes string concatenation in this normal mode StringBuilder sb = new StringBuilder (); for (int I = 0; I < 10; iCompiler +) {/ / in the loop, the Java compiler cannot optimize, so use StringBuilder sb.append (I) manually;}

6. Use Set if you need to call the Collection.contains method frequently

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

Counterexample:

/ / frequently call Collection.contains () counterexample List list = new ArrayList (); for (int I = 0; I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report