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 Java to improve code performance

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

Share

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

This article mainly introduces "how to write Java to improve code performance". In daily operation, I believe many people have doubts about how to write Java to improve code performance. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how to write Java to make code performance higher". Next, please follow the editor to study!

When you need the primary key and values of Map, you should iterate over entrySet ()

Iterating over keySet () is correct when only the primary key of Map is needed in the loop. However, iterating over entrySet () is more efficient when primary keys and values are needed, and it is better to iterate over keySet () and then get values.

Counterexample:

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

Positive example:

Map map =...; for (Map.Entry entry: map.entrySet ()) {String key = entry.getKey (); String value = entry.getValue ();...}

Should use Collection.isEmpty () to detect null

Using Collection.size () to detect null is logically fine, but using Collection.isEmpty () makes the code easier to read and better performance. The time complexity of any Collection.isEmpty () implementation is O (1), but some Collection.size () implementations may be O (n).

Counterexample:

If (collection.size () = = 0) {...}

Positive example:

If (collection.isEmpty ()) {...}

If you also need to detect null, use CollectionUtils.isEmpty (collection) and CollectionUtils.isNotEmpty (collection).

Don't pass the collection object to yourself.

In addition, because some methods require parameters to remain unchanged during execution, passing the collection to itself may result in abnormal behavior.

Counterexample:

List list = new ArrayList (); list.add ("Hello"); list.add ("World"); if (list.containsAll (list)) {/ / meaningless, always returns true...} list.removeAll (list); / / poor performance, directly use clear ()

Set initialization to specify the size as much as possible

The collection class of java is very convenient to use, but as you can see from the source code, the collection is also limited in size. The time complexity of each expansion is likely to be O (n), so specifying a predictable set size as far as possible can reduce the number of set expansion.

Counterexample:

Int [] arr = new int [] {1,2,3}; List list = new ArrayList (); for (int I: arr) {list.add (I);}

Positive example:

Int [] arr = new int [] {1,2,3}; List list = new ArrayList (arr.length); for (int I: arr) {list.add (I);}

String concatenation using StringBuilder

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

Counterexample:

String s = "; for (int I = 0; I < 10; iTunes +) {s + = I;}

Positive example:

String a = "a"; String b = "b"; String c = "c"; String s = a + b + c; / / No problem, the java compiler will optimize StringBuilder sb = new StringBuilder (); for (int I = 0; I < 10; iTunes +) {sb.append (I); / / in the loop, the java compiler cannot optimize, so use StringBuilder} manually

Random access to List

Everyone knows the difference between arrays and linked lists: random access to arrays is more efficient. After calling the method to get the List, what if you want to randomly access the data in it and don't know whether the internal implementation of the array is a linked list or an array? You can determine whether it implements the * RandomAccess * interface.

Positive example:

/ / call someone else's service to get list List list = otherService.getList (); if (list instanceof RandomAccess) {/ / internal array implementation, you can randomly access System.out.println (list.get (list.size ()-1));} else {/ / internal may be linked list implementation, random access is inefficient}

To call the Collection.contains method frequently, use Set

In the java collection class library, the general time complexity of List's contains method is O (n). If you need to call the contains method to find data frequently in the code, you can first convert list into HashSet implementation, reducing the time complexity of O (n) to O (1).

Counterexample:

ArrayList list = otherService.getList (); 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