[Java Basics] Calculate the total length of the array in the string array (StringUtils.join & StringBuilder.append)

Preface

Recently, when I was developing, I needed to calculate the length of a String array after splicing. I originally planned to write a simple tool class myself to calculate the length. After a colleague reminded me, I found that there is such a useful tool class
.


method one

    public static int calculateStrJoinLengthOfListMethod1(List<String> strList){
    
    
        if(CollectionUtils.isEmpty(strList)){
    
    
            return 0;
        }
        // 数组的join方法 “[Hello,world,abc]”
        String tmpStr1 = StringUtils.join(strList);

        // 注意此处我们不希望使用默认的分隔符 "," "HelloWorldAbc"
        String tmpStr2 = StringUtils.join(strList, "");
        return tmpStr2.length();
    }

Method Two

    public static int calculateStrJoinLengthOfListMethod2(List<String> strList){
    
    
        if(CollectionUtils.isEmpty(strList)){
    
    
            return 0;
        }
        StringBuilder builder = new StringBuilder();
        strList.forEach(str -> builder.append(str));
        return builder.toString().length();
    }

Test Methods

package com.yanxml.util.string;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;


public class StringArrayUtils {
    
    

    public static int calculateStrJoinLengthOfListMethod1(List<String> strList){
    
    
        if(CollectionUtils.isEmpty(strList)){
    
    
            return 0;
        }
        // 数组的join方法 “[Hello,world,abc]”
        String tmpStr1 = StringUtils.join(strList);

        // 注意此处我们不希望使用默认的分隔符 "," "HelloWorldAbc"
        String tmpStr2 = StringUtils.join(strList, "");
        return tmpStr2.length();
    }


    public static int calculateStrJoinLengthOfListMethod2(List<String> strList){
    
    
        if(CollectionUtils.isEmpty(strList)){
    
    
            return 0;
        }
        StringBuilder builder = new StringBuilder();
        strList.forEach(str -> builder.append(str));
        return builder.toString().length();
    }

    public static void main(String[] args) {
    
    
        List<String> strList = new ArrayList<>();
        strList.add("Hello");
        strList.add("World");
        strList.add("abc");

        // 测试方法1
        int lengthByTest1 = calculateStrJoinLengthOfListMethod1(strList);
        System.out.println("Test By Method1 - StringUtils.join, length " + lengthByTest1);

        // 测试方法2
        int lengthByTest2 = calculateStrJoinLengthOfListMethod2(strList);
        System.out.println("Test By Method2 - StringBuilder.append, length "+ lengthByTest2);  

    }
}
# 测试结果

Test By Method1 - StringUtils.join, length 13
Test By Method2 - StringBuilder.append, length 13

Source code analysis

From a user's perspective, it should normally end here. However, from actual development, we have all learned that when we encounter problems, if we go deeper, we will have better understanding and rewards
.

Let’s take a closer look at the method first StringUtils.join.

Join method of array
 String tmpStr1 = StringUtils.join(strList);
# org.apache.commons.lang3.StringUtils 类

	# 单参数重载方法 Array
	public static <T> String join(T... elements) {
    
    
		return join((Object[])elements, (String)null);
	}
	
	# 2个参数重载方法 Array & 间隔符
    public static String join(Object[] array, String separator) {
    
    
        return array == null ? null : join(array, separator, 0, array.length);
    }


	# 4个参数重载方法 Array & 间隔符 & 开始下标 & 结束下标
    public static String join(Object[] array, String separator, int startIndex, int endIndex) {
    
    
        if (array == null) {
    
    
            return null;
        } else {
    
    
            if (separator == null) {
    
    
                separator = "";
            }

            int noOfItems = endIndex - startIndex;
            if (noOfItems <= 0) {
    
    
                return "";
            } else {
    
    
                StringBuilder buf = new StringBuilder(noOfItems * 16);

                for(int i = startIndex; i < endIndex; ++i) {
    
    
                    if (i > startIndex) {
    
    
                        buf.append(separator);
                    }

                    if (array[i] != null) {
    
    
                        buf.append(array[i]);
                    }
                }

                return buf.toString();
            }
        }
    }
  • As you can see, this join method is the same as the two methods. The method used StringBuilder.appendis just a layer of packaging and has no other use. And both of them are thread-unsafe.

Join method of string
        String tmpStr2 = StringUtils.join(strList, "");
# org.apache.commons.lang3.StringUtilspublic static String join(Iterable<?> iterable, String separator) {
    
    
        return iterable == null ? null : join(iterable.iterator(), separator);
    }
    public static String join(Iterator<?> iterator, String separator) {
    
    
        if (iterator == null) {
    
    
            return null;
        } else if (!iterator.hasNext()) {
    
    
            return "";
        } else {
    
    
            Object first = iterator.next();
            if (!iterator.hasNext()) {
    
    
                String result = ObjectUtils.toString(first);
                return result;
            } else {
    
    
                StringBuilder buf = new StringBuilder(256);
                if (first != null) {
    
    
                    buf.append(first);
                }

                while(iterator.hasNext()) {
    
    
                    if (separator != null) {
    
    
                        buf.append(separator);
                    }

                    Object obj = iterator.next();
                    if (obj != null) {
    
    
                        buf.append(obj);
                    }
                }

                return buf.toString();
            }
        }
    }
  • What's more interesting is that the results obtained by these two methods are completely different. The main problem may be here, the iteration of the array is through a forced conversion, and the string is processed return join((Object[])elements, (String)null);using Iterator.

a strange phenomenon

The result is "[Hello, World, Abc]"

        // 数组的join方法 “[Hello,world,abc]”
        String tmpStr1 = StringUtils.join(strList);
        // 注意此处我们不希望使用默认的分隔符 "," "HelloWorldAbc"
        String tmpStr2 = StringUtils.join(strList, "");
  • It may be that the first calling method triggered the array method, which caused this display toStringto appear here .[XX,XX,XX]

Reference

[1]. [Method and use of StringUtils.join() method] ( https://www.cnblogs.com/fenghh/p/12175368.html )
[2]. [Which package CollectionUtils belongs to] https://blog .csdn.net/weixin_42114097/article/details/90579980

Guess you like

Origin blog.csdn.net/u010416101/article/details/123038426