编程语言
首页 > 编程语言> > 【Java】guava(五) splitter joiner

【Java】guava(五) splitter joiner

作者:互联网

List使用:

@Test
    public void testList(){
        List<String> list = Splitter.on("-")
                .splitToList("A-B-C");
        System.out.println(list);

        List<String> list1 = new ArrayList<>(list);
        list1.add(null);
        list1.add("D");
        String s = Joiner.on("@")
                .skipNulls() // 如果有null会报错
                .join(list1);
        System.out.println(s);
    }

[A, B, C]
A@B@C@D

 

Map使用:

    @Test
    public void testMap(){
        Map<String, String> map = Splitter.on("-")
                .withKeyValueSeparator("=")
                .split("A=1-B=2-C=3");
        System.out.println(map);

        String s = Joiner.on("@")
                .withKeyValueSeparator(":")
                .join(map);
        System.out.println(s);
    }

{A=1, B=2, C=3}
A:1@B:2@C:3

标签:splitter,Java,map,list,list1,System,println,guava,out
来源: https://blog.csdn.net/u010900754/article/details/97499941