Guava学习笔记--01. Joiner使用

付威     2019-08-30   2835   8min  

对于Guava的认识来自于阅读QMQ源码的时候,有很多没有使用过的Java方法和集合,所以就查了查,发现是用的是Guava的类库,所以就有了下面的博客:

1. 字符串的拼接

 
    private final List<String> stringList = Arrays.asList("Google", "Guava", "Java", "Scala", "Kafka");
    private final List<String> stringListWithNullValue = Arrays.asList("Google", "Guava", "Java", "Scala", null);


     //1. 字符串的拼接
    @Test
    public void Test() {
        String join = Joiner.on("*").join(stringList);
        System.out.println(join);
        assertEquals(join, "Google*Guava*Java*Scala*Kafka");
    }

    @Test(expected = NullPointerException.class)
    public void TestJoinValue() {
        String join = Joiner.on("*").join(stringListWithNullValue);
        System.out.println(join);
        assertEquals(join, "Google*Guava*Java*Scala*Kafka");
    }

2. 跳过空数据

    //2. 跳过空数据
    @Test
    public void TestJoinSkipValue() {
        String join = Joiner.on("*").skipNulls().join(stringListWithNullValue);
        System.out.println(join);
        assertEquals(join, "Google*Guava*Java*Scala");
    }

3. 为空的数据指定一个默认值

     //3. 为空的数据指定一个默认值
    @Test
    public void TestJoinDefalutValue() {
        String join = Joiner.on("*").useForNull("Defalut").join(stringListWithNullValue);
        System.out.println(join);
        assertEquals(join, "Google*Guava*Java*Scala*Defalut");
    }

4. 追加到StringBuilder

    //4. 追加到StringBuilder
    @Test
    public void TestJoinDefalutValueStringBuffer() {
        StringBuilder stringBuilder = new StringBuilder();
        StringBuilder thatBuilder = Joiner.on("*").useForNull("Defalut").appendTo(stringBuilder, stringListWithNullValue);
        assertThat(thatBuilder, sameInstance(stringBuilder));
        assertThat(stringBuilder.toString(), equalTo("Google*Guava*Java*Scala*Defalut"));
        assertThat(thatBuilder.toString(), equalTo("Google*Guava*Java*Scala*Defalut"));
    }

5. 追加到文件

    //5. 追加到文件
    @Test
    public void TestJoinDefalutValueToWrite() {

        try (FileWriter fileWriter = new FileWriter(new File("1.txt"))) {
            Joiner.on("#").useForNull("Default").appendTo(fileWriter, stringListWithNullValue);
            assert (Files.isFile().test(new File("1.txt")));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

6. 使用Java8实现相同的功能

    //java8
    @Test
    public void TestJoinDefalutByStream() {
        String result = stringListWithNullValue.stream().map(x -> x == null || x.isEmpty() ? "Defalut" : x).collect(Collectors.joining("*"));
        assertThat(result, equalTo("Google*Guava*Java*Scala*Defalut"));
    }

7. 使用Guava对Map进行join

    @Test
    public void testJoinOnWithMap(){
         //创建一个map
        Map<String,String> stringMap= ImmutableMap.of("Hello","Guava","Java","Scala"); 
        String join = Joiner.on("#").withKeyValueSeparator("=").join(stringMap);
        System.out.println(join);
        assert join.equals("Hello=Guava#Java=Scala");
    }
 
(本文完)

作者:付威

博客地址:http://blog.laofu.online

如果觉得对您有帮助,可以下方的RSS订阅,谢谢合作

如有任何知识产权、版权问题或理论错误,还请指正。

本文是付威的网络博客原创,自由转载-非商用-非衍生-保持署名,请遵循:创意共享3.0许可证

交流请加群113249828: 点击加群   或发我邮件 laofu_online@163.com

付威

获得最新的博主文章,请关注上方公众号