Such specification writing code

Interview Collection, quickly went to collect it! offer.liangsonghua.me/ . Focus on micro-channel public number: preserved egg blackboard, get more exciting!

 

 

A, iteration entrySet () Retrieves the Map of the key and value

When the loop only needs to acquire Map primary key key, iteration keySet () is correct; however, when you need a primary key key and value value, iteration entrySet () is a more efficient approach, which keySet than the first iteration () by then go get the value of better performance.

Counterexample:

 

 

 

Positive examples:

 

 

 

Second, the use Collection.isEmpty () detects an empty

Use of a Collection, () is empty is not detected on the logical problems, but using Collection.isEmpty () makes the code more readable, better performance can be obtained; in addition, any Collection.isEmpty () implemented time complexity is O (1), multiple cycles need not traverse, but by a certain time complexity of a Collection, () method implementation may be O (n)

Counterexample:

 

 

 

Positive examples:

 

 

 

Third, as far as possible the size specified set of initialization

Try to set the size specified at initialization, can effectively reduce the number of sets of expansion, because each set of expansion when the time complexity is likely to O (n), and time-consuming performance.

Counterexample:

 

 

 

Positive examples:

 

 

 

Fourth, the use StringBuilder string concatenation

General string concatenation will be at compile Java optimization, but splice Java compiler optimization of the string can not be executed in a loop, so StringBuilder need to be replaced.

Counterexample:

 

 

 

Positive examples:

 

 

 

V. To recall Collection.contains method frequently use the Set

In the Java collection class library, List contains the general method of time complexity of O (n), if the code contains method calls frequently find the first set of data is converted into a list HashSet achieved, the O (n) time complexity will be O (1).

Counterexample:

 

 

 

Positive examples:

 

 

 

Sixth, the use of static code block implements static member variable assignment

For static member variables collection types, you should use static code block assignment, instead of using a set of implementation to evaluation.

Counterexample:

 

Positive examples:

 

 

 

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

IX tools shield constructor

Tools is a collection of a bunch of static fields and functions, which should not be instantiated; however, Java adds an implicit public constructor for each class is not explicitly defined constructor, in order to avoid unnecessary instantiation, should explicit definition of private constructors to shield the implicit public constructor.

Counterexample:

 

 

 

Positive examples:

 

 

 

Ten, remove the extra exception caught and ran

After capturing an exception with catch phrase, if nothing is processed, it just makes an exception is thrown again, this result is not like catching exceptions, you can delete or add another piece of code that process.

Counterexample:

 

 

 

 

Positive examples:

 

 

 

Eleven, the string conversion using String.valueOf (value) instead of "" + value

When the type or other objects into a string, using String.valueOf (value) "" + is higher than the efficiency value.

Counterexample:

 

 

 

Positive examples:

 

 

 

Twelve, avoid using BigDecimal (double)

BigDecimal (double) the presence of risk of loss of precision, the exact calculation of the comparison values ​​or the scene may cause the business logic exception.

Counterexample:

 

 

 

Positive examples:

 

 

 

XIII, returns an empty array rather than null and collections

If the program run returns null, null square mandatory testing needs to be called, otherwise it will throw null pointer exception; returns an empty array or an empty set, effectively avoid the situation because the caller does not detect null null pointer exception is thrown, you can also delete the caller detect null statement makes the code more concise.

Counterexample:

 

 

 

Positive examples:

 

 

 

Fourth, priority use constant or call equals method to determine the value

equals method of Object readily shorting pointer exception, should be used with a constant value or determined object to call the equals method.

Counterexample:

 

 

 

Positive examples:

 

 

 

 

Fifth, the enumeration of attribute fields must be private and immutable

Enumeration is generally used as a constant, or if the common attribute field setting field enumeration methods exist, then the enumeration constant properties can easily be modified; ideally, enumeration property fields are private, proprietary and constructor, assignment, there is no corresponding method Setter, preferably together with the final modifier.

Counterexample:

 

 

 

Positive examples:

 

 

 

Sixteen, tring.split (String regex) part of a keyword needs translation

When using plit string String method, passing in the delimited string is a regular expression, then part of the keyword (such as [] () |., Etc.) need to be escaped.

Counterexample:

 

 

 

Positive examples:

 

Guess you like

Origin www.cnblogs.com/cider/p/11790248.html