工作中经常需要做一个List<String>转成一个用“,”隔开的String类型,一开始只是使用别人编写的StringUtils中listToString方法
代码如下
/**
* 将字符串的集合用逗号间隔拼接为字符串
* @param strList 字符串的集合
* @return
*/
public static String listToString(List<String> strList){
String str="";
if(CollectionUtils.isNotEmpty(strList)){
for (String s:strList) {
str= org.apache.commons.lang3.StringUtils.isBlank(s)?s:str+","+s;
}
}
return str;
}
之后有学习到一些Java8的新特性其中有String.join方法以上功能可以通过String.join(",", strList)实现。
既然用到了String.join这个方法就稍微研究一下StringUtils.listToString和String.join两者比较哪个较为优秀。
于是我查看了String.join的源码,代码如下
/**
* Returns a new {@code String} composed of copies of the
* {@code CharSequence elements} joined together with a copy of the
* specified {@code delimiter}.
*
* <blockquote>For example,
* <pre>{@code
* List<String> strings = new LinkedList<>();
* strings.add("Java");strings.add("is");
* strings.add("cool");
* String message = String.join(" ", strings);
* //message returned is: "Java is cool"
*
* Set<String> strings = new LinkedHashSet<>();
* strings.add("Java"); strings.add("is");
* strings.add("very"); strings.add("cool");
* String message = String.join("-", strings);
* //message returned is: "Java-is-very-cool"
* }</pre></blockquote>
*
* Note that if an individual element is {@code null}, then {@code "null"} is added.
*
* @param delimiter a sequence of characters that is used to separate each
* of the {@code elements} in the resulting {@code String}
* @param elements an {@code Iterable} that will have its {@code elements}
* joined together.
*
* @return a new {@code String} that is composed from the {@code elements}
* argument
*
* @throws NullPointerException If {@code delimiter} or {@code elements}
* is {@code null}
*
* @see #join(CharSequence,CharSequence...)
* @see java.util.StringJoiner
* @since 1.8
*/
public static String join(CharSequence delimiter,
Iterable<? extends CharSequence> elements) {
Objects.requireNonNull(delimiter);
Objects.requireNonNull(elements);
StringJoiner joiner = new StringJoiner(delimiter);
for (CharSequence cs: elements) {
joiner.add(cs);
}
return joiner.toString();
}
其注释的上半部分,有该join方法的使用例子,下半部分是一些参数解释和返回、抛出的异常等
在看源码时我又注意到有个StringJoiner这个工具类,我们对此类的操作就是joiner.add(cs)在看其add方法的源码如下
/**
* Adds a copy of the given {@code CharSequence} value as the next
* element of the {@code StringJoiner} value. If {@code newElement} is
* {@code null}, then {@code "null"} is added.
*
* @param newElement The element to add
* @return a reference to this {@code StringJoiner}
*/
public StringJoiner add(CharSequence newElement) {
prepareBuilder().append(newElement);
return this;
}
此处没什么好说的直接看prepareBuilder().append(newElement)他的源码如下
@Override
public StringBuilder append(CharSequence s) {
super.append(s);
return this;
}
我们看到它返回的是一个StringBuilder由此我们基本可以得出这一系列操作是for循环将list遍历将其中的元素加入到StringBuilder中(以什么符号作为间隔可以自选)在通过toString转成一个String来达到list转String的。
因为我们都知道StringBuilder是对本对象的操作,而String操作是将旧对象放入新对象,所以StringBuilder操作效率等比String好。
综上所述我们用String.join运行效率等比StringUtils.listToString好。我们应该用String.join代替原来的StringUtils.listToString。