list與字串轉換

EPIHPANY發表於2024-10-11
public class Test {

    @org.junit.Test
    public void test1(){
        List<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        // 分隔符可自定義
        String delemiter = ",";
        // list轉string
        String str = list.stream().collect(Collectors.joining(delemiter));
        System.out.println(str);
        // string轉換成list
        List<String> stringList = Arrays.asList(str.split(delemiter));
        System.out.println(stringList);
    }
}

相關文章