Tips: pay attention to public number: songhua preserved egg bulletin board, receive programmer monthly salary 25K+ secrets, march into BAT essential!

Iterates entrySet() to obtain 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:



Is:



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:



Is:



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:



Is:



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:



Is:



5. 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:



Is:



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:



Is:



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:



Is:



Delete the redundant exception catch and run out

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:



Is:



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:



Is:



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:



Is:



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:



Is:



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:



Is:



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:



Is:



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:



Is:



Source: www.liangsonghua.me

Pay attention to wechat public number: songhua preserved egg bulletin board, get more exciting!

Introduction to our official account: We share our technical insights from working in JD, as well as JAVA technology and best practices in the industry, most of which are pragmatic, understandable and reproducible