List转String学习感受

工作中经常需要做一个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。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值