Java的Set转成逗号分隔的字符串

2021-12-07 13:40:05 浏览数 (1)

代码语言:javascript复制
package com.javaedge.collection.set;

import org.apache.commons.lang.StringUtils;

import java.util.HashSet;
import java.util.Set;

/**
 * Set转逗号分隔字符串
 *
 * @author JavaEdge
 * @date 2021/10/8
 */
public class Set2String {

    public static void main(String[] args) {
        Set<String> set = new HashSet<>();
        set.add("Java");
        set.add("Edge");
        set.add("公众号");
        System.out.println(set);

        Object[] array = set.toArray();
        String splitSet = StringUtils.join(array);
        System.out.println("splitSet:"   splitSet);

        String splitSetWithComma = StringUtils.join(array, ",");
        System.out.println("splitSetWithComma:"   splitSetWithComma);
    }
}

0 人点赞