Java List转字符串,Set转字符串,集合转字符串,数组转集合

集合转字符串 :

 Set set1 = new HashSet<>();
        set1.add("a");
        set1.add("b");
        System.out.println(StringUtils.join(set1.toArray(), ","));//a,b

        List list1 = new ArrayList<>();
        System.out.println(StringUtils.join(list1.toArray(), ","));//

        list1.add("c");
        list1.add("d");
        System.out.println(StringUtils.join(list1.toArray(), ","));//c,d

数组转集合:


        //org.apache.commons.lang3
        Set shopIdSet = new HashSet<>();
        String s1 = StringUtils.join(shopIdSet.toArray(), ",");
        System.out.println(s1);//""

        shopIdSet.add("1");
        shopIdSet.add("2");
        s1 = StringUtils.join(shopIdSet.toArray(), ",");
        System.out.println(s1);//1,2
        
        String[] array = new String[]{"a", "b", "a"};
        System.out.println(new HashSet<>(Arrays.asList(array)));//[a, b]

工具类:

package org.apache.commons.lang3;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.text.Normalizer;
import java.text.Normalizer.Form;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.regex.Pattern;

public class StringUtils {
.............

}

 

你可能感兴趣的:(Java,List转字符串,Set转字符串,集合转字符串,数组转集合)