1. The purpose of our doing this:
1. Efficiency (most important)
2. Readability, easy to maintain in the later stage. (Also important)
2. Requirements for code optimization:
1. Reduce the volume of the code.
2. Improve the running efficiency of the code.
3. Optimization of commonly used code:
1. Try to reuse objects:
Especially the reuse of String objects. The most commonly used string splicing:
When encountering frequent wiping of String. Remember to use StringBuilder/StringBuffer
For example:
ArrayList<String> list; //Save list initialization. StringBuilder builder = new StringBuilder(); for (String s : list) { builder.append(s); } String result = builder.toString();Reason: Java virtual machines not only need to spend time generating objects, but also need to spend time processing and recycling objects. Generating too many objects will definitely have an impact on program performance.
2. Use local variables where possible:
Local variables are created in the stack, and are created quickly and disappear automatically after use, without additional garbage collection.
Static variables, instance variables, etc. are created in the heap, and the creation speed is slow, and it also relies on the Java garbage collection mechanism to process.
3. Close the flow in time:
In Java program development, after the I/O and database operations are completed, you must remember to close the flow.
Cause: Not closing the flow can cause great overhead to the system and may even have serious consequences for the data.
4. Use lazy loading
Lazy loading: Create this object only when it is to be used.
For example:
String prefix = "gebi"; if ("laowang".equals(name)) { list.add(prefix + name); }Replace with:
if("laowang".equals(name)) { String prefix = "gebi"; list.add(prefix + name); }5. Avoid using try...catch in loops, use try...catch outside the loop
6.try...catch should not be too big.
Don't put all the useless code, that is, the exception will not be thrown into the try...catch block, and reduce the size of the try...catch code block.
Ensure the readability, maintenance and robustness of the code.
7. Try to avoid creating references to objects within the loop.
Especially when the circulation volume is large.
while (i<1000) { Object object = new Object(); }It is recommended to modify it to:
Object object = null; while (i<1000) { object = new Object();Every time new Object() is new, the Object object reference points to the Object object.
When there are many loops, for example, the first type, the JVM will create 1,000 references to objects, while the second type only has one Object object reference in memory. This greatly saves memory space.
8. Do not use static variables at will.
When an object is referenced by a variable declared static, the Java garbage collector will not clean up the heap memory occupied by the object.
The heap memory occupied by the static variable is not released until the program where the variable is located. That is, static variable life cycle = class life cycle.
9. Do not create some unused objects, and do not import some unused classes.
10. Use buffered I/O streams:
Buffered I/O streams can greatly improve I/O efficiency. BufferedWriter, BufferedReader, BufferedInputStream, BufferedOutputStream.
11. Convert wrapping class data to strings to use: toString
Integer i = 1;
Speed ranking of method conversion of wrapper class data to string:
i.toString > String.valueOf(i) > "" + i
12.Map traversal efficiency: entrySet > keySet
//entrySet() for (Entry<String, String> entry : map.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); System.out.pr intln(key + " : " + value); } //Compare up and down//keySet() for (String key : map.keySet()) { String value = map.get(key); System.out.println(key + " : " + value ); }13. About the collection traversal of Iterator and forEach().
Introduction to the algorithm says: Algorithms are intended to improve space efficiency and time efficiency. But often time and space cannot coexist.
Time efficiency: Iterator > forEach()
Code readability: forEach() > Iterator
//Iterator Set<Entry<String, String>> entrySet = map.entrySet(); Iterator<Entry<String, String>> iter = entrySet.iterator(); while (iter.hasNext()) { Entry<String, String> entry = iter.next(); String key = entry.getKey(); String value = entry.getValue(); System.out.println(key + " : " + value); }contrast:
//forEach() for (Entry<String, String> entry : map.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); System.out.print ln(key + " : " + value); }Personally, I believe that it is recommended to use Iterator to traverse the collection when processing big data.
However, for small data processing, forEach() is still used for readability and later maintenance.
Both should be mastered when using them in combination.