前言
最近在开发的时候, 需要计算一个String数组, 拼接后的长度. 本来是准备自己写了一个简单的工具类, 计算长度. 经过同事的提醒, 发现还有这样一个好用的工具类.
方法一
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();
}
测试方法
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
源码解析
从使用者的角度来说, 正常到这里就应该结束了. 但是, 从实际开发中, 我们都学到了, 遇到问题, 多深入一点, 就会有更好的理解和回报.
我们先仔细看下StringUtils.join
方法.
数组的Join方法
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();
}
}
}
- 可以看到, 这个join方法就是使用的方法二方法一样. 用的
StringBuilder.append
方法. 只是包装了一层, 并无其他卵用. 并且还是2者都是线程非安全的.
字符串的Join方法
String tmpStr2 = StringUtils.join(strList, "");
# org.apache.commons.lang3.StringUtils 类
public 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();
}
}
}
- 比较有意思的是, 这2个方法得到的结果是完全不一样的. 最主要的问题, 可能就是在这里的, 数组的迭代是通过一个强制转换上
return join((Object[])elements, (String)null);
字符串是使用Iterator
进行处理的.
一个奇怪的现象
得到的结果为 “[Hello, World, Abc]”
// 数组的join方法 “[Hello,world,abc]”
String tmpStr1 = StringUtils.join(strList);
// 注意此处我们不希望使用默认的分隔符 "," "HelloWorldAbc"
String tmpStr2 = StringUtils.join(strList, "");
- 可能是第一种调用方式, 触发了数组的
toString
方法, 导致了这里会出现,[XX,XX,XX]
这样的展现.
Reference
[1]. [StringUtils.join()方法的方法和使用] (https://www.cnblogs.com/fenghh/p/12175368.html)
[2]. [CollectionUtils属于哪个包] https://blog.csdn.net/weixin_42114097/article/details/90579980