A summary of Ali's JAVA development manual

A summary of Ali's JAVA development manual

Some knowledge points of Alibaba java development manual are incomplete, but they are all more important

OOP Statute

1. An outdated interface must be annotated with @Deprecated and clearly state what new interface or new service is used

2. Comparison of values ​​between all objects of the same type of packaging class, all using the equals method for comparison. Note: For the assignment of Integer var =? In the range of -128 to 127, the Integer object is generated in IntegerCache.cache, which will reuse the existing object. The Integer value in this interval can be directly judged using ==, but this interval All data other than those will be generated on the heap and will not reuse existing objects. This is a big pit. It is recommended to use the equals method to judge.

Aggregate processing

1. Do not remove / add elements in the foreach loop. Use the Iterator method for the remove element. If you operate concurrently, you need to lock the Iterator object.
Positive example:

Iterator<String> it = a.iterator();
while (it.hasNext()) {
    String temp = it.next();
    if (删除元素的条件) {
        it.remove();
    }
}

Counterexample:

List<String> a = new ArrayList<String>();
a.add("1");
a.add("2");
for (String temp : a) {
    if ("2".equals(temp)) {
        a.remove(temp);
    }
}

Explanation: The execution result of the above code will definitely be beyond everyone's expectations, so try replacing "1" with "2", will it be the same result?
Counter-examples will report an error. The first time that no error is reported is because after "1" is removed, it.corsor = 1 size = 1 it. Next method retrun corsor! = Size returns false. The end of the cycle
a second time to remove "2" after
it.corsor = 2 size = .Next method 1 it's retrun corsor! = Size returns true. Executed it. The next next method finds that the collection has been modified and throws an exception * java.util.ConcurrentModificationException

2. When initializing the collection, specify the initial value of the collection. Note: HashMap is initialized with HashMap (int initialCapacity), positive example: initialCapacity = (number of elements to be stored / load factor) + 1. Note that the load factor (ie loader factor) defaults to 0.75. If the initial value cannot be determined temporarily, set it to 16. Counter example: HashMap needs to place 1024 elements. Since the initial size of the capacity is not set, as the elements continue to increase, the capacity is forced to expand 7 times. Resize needs to rebuild the hash table, which seriously affects performance

3. Use entrySet to traverse the Map class set KV instead of keySet. Explanation: The keySet is actually traversed 2 times, once it is converted to Iterator object, and the other time is to extract the value corresponding to the key from the hashMap. The entrySet only traverses once and puts the key and value into the entry, which is more efficient. If it is JDK8, use the Map.foreach method. Positive example: values ​​() returns a set of V values, which is a list collection object; keySet () returns a set of K values, which is a Set collection object; entrySet () returns a combined set of KV values

4. HashMap may have a dead chain due to high concurrency when the capacity is not enough for resize, causing the CPU to soar. You can use other data structures or locks to avoid this risk during the development process.

MySQL database

Index protocol

1. More than three tables are prohibited from joining. For the fields that need to be joined, the data types must be absolutely consistent; when multi-table related queries, ensure that the related fields need to have indexes. Note: Even with dual table joins, attention should be paid to table indexes and SQL performance.

2. The page search is strictly forbidden left blur or full blur, if necessary, please go to the search engine to solve. Note: The index file has the left-most prefix matching feature of B-Tree. If the value on the left is not determined, this index cannot be used.

3. Use the covering index to perform query operations and avoid returning to the table. Explanation: If a book needs to know what the title of Chapter 11 is, will the page corresponding to Chapter 11 be opened? Just browse the directory, this directory is used to cover the index. Positive example: The types of indexes that can be established: primary key index, unique index, ordinary index, and covering index is an effect of a query. With the result of explain, the extra column will appear: using index

4. Prevent implicit conversion caused by different field types, leading to index failure

5. Use ISNULL () to determine whether it is a NULL value. Note: The direct comparison of NULL with any value is NULL. Explanation: 1) The return result of NULL <> NULL is NULL, not false. 2) The return result of NULL = NULL is NULL, not true. 3) The return result of NULL <> 1 is NULL, not true.

6. It is forbidden to use stored procedures, which are difficult to debug and expand, and even less portable.

7. If the in operation can be avoided, avoid it. If it is unavoidable, you need to carefully evaluate the number of set elements behind in and control it to within 1,000

Published 17 original articles · won 24 · views 280,000 +

Guess you like

Origin blog.csdn.net/qq_22956867/article/details/76628499