StringUtils.join,原集合为空,结果不为空


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

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

/**
 * @Desc
 **/
public class Test8 {


    public static void main(String[] args) {
        List list = new ArrayList();

        System.out.println(list == null);//false

        String s = StringUtils.join(list, ",");//一定要注意,此时字符串s不为空,是""。吃了大亏!
        System.out.println(s == null);//false
        System.out.println(s.length());//0

        s = CollectionUtils.isEmpty(list) ? null : StringUtils.join(list, ",");//先判断原list是否为空
        System.out.println(s == null);//true

        list.add("a");
        s = StringUtils.join(list, ",");
        System.out.println(s);//a


        list.add("b");
        list.add("c");
        s = StringUtils.join(list, ",");
        System.out.println(s);//a,b,c
    }
}

 

你可能感兴趣的:(Java)