[Java] Summary of Knowledge Points (1)

I reorganized the past notes on the weekend, and found that many Java-related experiences were recorded in txt or word, so I sorted out all of them and put them on the Internet for a record.

1. Use equals for wrapper classes, and == for base classes

in conclusion

Integer uses equals, int uses ==

analyze
  1. Comparing the basic type and the basic package type with the "==" operator, the basic package type will be automatically unboxed into the basic type before comparison, so Integer(0) will be automatically unboxed to int type before comparison .
  2. When the other two Integer objects are compared with "==", if one of the Integer objects is obtained by new, return false, because the addresses of the two objects are compared.
  3. Two basic types of encapsulation types are compared by equals(). First, equals() will compare the types. If the types are the same, then continue to compare the values. If the values ​​are also the same, return true.
  4. The basic encapsulation type calls equals(), but the parameter is a basic type. At this time, it will be automatically boxed first, and the basic type will be converted to its encapsulation type. If the type is different, false will be returned.
  5. If the boxed types are the same, compare the values, and return true if the values ​​are the same, otherwise return false.

2. Regular expression precompilation and use

in conclusion

Using the precompilation function of regular expressions can effectively speed up the regular matching speed. Pattern should be defined as a static final static variable to avoid performing multiple precompilations.

analyze
private static final Pattern pattern = Pattern.compile(regexRule); 
private void func(...) {
    
     
    Matcher m = pattern.matcher(content); 
    if (m.matches()) {
    
     
        ... 
    } 
}

3. HashMap initialization size improves performance

in conclusion

Performance can be improved by setting the initialization size of the HashMap.

analyze

HashMap triggers rehash when the capacity of the internally maintained hash table reaches 75%. So the initialization capacity should be set to num/3x4 instead of num. This article is also written in Ali's java development manual, and in order to offset the rounding error, Ali recommends that the initial capacity be set to num/3x4+1.

initialCapacity = expectedSize / 0.75F + 1.0F

The specific application is

int size = list.size();
double hashSize = size/0.75+1;
Map<Integer, Integer> map = new HashMap<>(hashSize);

4. The difference between Y and y in SimpleDateFormat

in conclusion
// 关于SimpleDateFormat的正确写法如下
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
analyze

y is Year, Y means Week year. Week year means the year that the week of the current day belongs to. A week starts on Sunday and ends on Saturday. As long as this week crosses the new year, this week will be counted as the next year.

5. Mybatis object nested writing

Entity contains entity writing

public class OrderInfoDto {
    
    
    ......
    //另一个对象SubOrderDto
    private  SubOrderDto subOrderDto;  
}

In this case, Mapper.xml should be written like this

<resultMap extends="BaseResultCommon" id="BaseResultMap" type="cn.paohe.finance.model.dto.OrderInfoDto">
    <id column="ID" jdbcType="BIGINT" property="id"/>
   ......
    <association property="subOrderDto" javaType="cn.paohe.finance.model.dto.SubOrderDto">
        <id column="sub_id" jdbcType="BIGINT" property="id"/>
        <result column="ORDER_ID" jdbcType="BIGINT" property="orderId"/>
        <result column="ORDER_CODE" jdbcType="VARCHAR" property="orderCode"/>
        <result column="ORDER_STATEMENT_ID" jdbcType="BIGINT" property="orderStatementId"/>
        <result column="SUB_ORDER_CODE" jdbcType="VARCHAR" property="subOrderCode"/>
    </association>
</resultMap>

The contained objects are also listed in the resultMap through the association tag.
Entity contains set notation

public class ProcurementOrderVo {
    
    
    ......  
    //含有另一个集合
    private List<RemakListVo> remarkList;
}

In this case, Mapper.xml should be written like this

<resultMap id="BaseResultMap" type="cn.paohe.finance.vo.ProcurementOrderVo">
    <id column="ID" jdbcType="BIGINT" property="id"/>
    ......
    <collection property="remarkList" ofType="cn.paohe.finance.vo.RemakListVo">
        <result column="REMARK" jdbcType="VARCHAR" property="remark"/>
        <result column="addUserId_k" jdbcType="BIGINT" property="addUserId"/>
        <result column="addUserName" jdbcType="VARCHAR" property="addUserName"/>
        <result column="addTime_k" jdbcType="DATE" property="addTime"/>
        <result column="oprTime_k" jdbcType="DATE" property="oprTime"/>
        <result column="ALIVE_FLAG" jdbcType="VARCHAR" property="aliveFlag"/>
    </collection>
</resultMap>

6. Apache BeanUtils is deprecated

in conclusion

Using ASM's mapped copy is the most reliable approach.

analyze

When we enable the Ali code scanning plug-in, if you use Apache BeanUtils.copyProperties to copy properties, it will give you a very serious warning.

Because the performance of Apache BeanUtils is poor, you can use Spring BeanUtils or Cglib BeanCopier instead. The results show that the copy speed of Cglib's BeanCopier is the fastest, and even a million copies only need 10 milliseconds! In comparison, the worst is the BeanUtils.copyProperties method of the Commons package, and the 100-copy test is as much as 400 times different from the best-performing Cglib. There is a performance difference of 2600 times in millions of copies!

Looking at the source code, we will find that CommonsBeanUtils mainly has the following time-consuming places:

  • Output a lot of log debugging information
  • Duplicate object type check​

In addition to performance issues, there are other pitfalls that require special care when using CommonsBeanUtils!

  • Wrapper class default value When copying properties, the lower version of CommonsBeanUtils will cause the original type of the target object to be assigned an initial value for the original type of the wrapper class property in order to solve the problem of Date being empty. For example, the default value of the Integer property is 0, although your source object should be The value of the field is null. This scenario, which has a special meaning when our wrapper class attribute is a null value, is very easy to step on! For example, the search criteria object, generally a null value means that the field is not limited, and 0 means that the value of the field must be 0.

  • When switching to other tools, when we see Ali’s tips and know the performance problems of CommonsBeanUtils, we should also be careful when we want to switch to Spring’s BeanUtils:

org.apache.commons.beanutils.BeanUtils.copyProperties(Object target, Object source);org.springframework.beans.BeanUtils.copyProperties(Object source, Object target);

It can be seen from the method signature that the names of the two tool classes are the same, the method names are also the same, and even the number, type, and name of the parameters are the same. But the positions of the parameters are reversed. Therefore, if you want to change, please remember to change the target and source parameters too!

Guess you like

Origin blog.csdn.net/kida_yuan/article/details/129487039