美文网首页
002 Guava | Utilities

002 Guava | Utilities

作者: __destory__ | 来源:发表于2019-02-27 16:46 被阅读0次

    本节介绍Guava的utilities中的工具包,

    1. Joiner
    2. Spliter
    3. Preconditions
    4. Objects
    5. Strings
    6. MoreObjects
    7. ComparisonChain
    8. CharMatcher
    9. StopWatcher
    10. ServerLoader

    1. Joiner

    负责将可迭代的集合,按照指定的分隔符,Join成一个字符串,其分隔符是指定的。

    private final List<String> str1 = Arrays.asList( "1", "2", "3", "4", "5");
    private final List<String> str2null = Arrays.asList( "1", "2", "3", "4", null);
    
    //用法1
    String result = Joiner.on("#").join(str1);
    assertThat(result, equalTo("1#2#3#4#5"));
    
    //用法2
    String result = Joiner.on("#").join(str2null);
    assertThat(result, equalTo("1#2#3#4"));  //会报错,因为join默认不能包含null
    
    //用法3,忽略null值
    String result = Joiner.on("#").skipNulls().join(str2null);
    assertThat(result, equalTo("1#2#3#4"));
    
    //用法4,使用默认值代替null值
    String result = Joiner.on("#").useForNull("DEFAULT").join(str2null);
    assertThat(result, equalTo("1#2#3#4#DEFAULT"));
    
    //用法5,appendTo到StringBuilder中
    final StringBuilder builder = new StringBuilder();
    StringBuilder resultBuilder = Joiner.on("#").useForNull("DEFAULT").appendTo(builder, str2null);
    assertThat(resultBuilder, sameInstance(builder));
    assertThat(resultBuilder.toString(), equalTo("1#2#3#4#DEFAULT"));
    assertThat(builder.toString(), equalTo("1#2#3#4#DEFAULT"));
    
    //用法6,appendTo到文件流中
    //注意try..catch..用法
    try (FileWriter writer = new FileWriter(new File(fileNameStr))) {
          //写入文件
         Joiner.on("#").useForNull("DEFAULT").appendTo(writer, str2null);
         //判断文件是否存在
         assertThat(Files.isFile().test(new File(fileNameStr)), equalTo(true));
    } catch (IOException e) {
         fail("Error.");
    }
    
    //用法7,对map进行join
    private final Map<String, String> stringMap = of("1", "2", "3", "4");
    String mapStr=Joiner.on('#').withKeyValueSeparator("=").join(stringMap);
    assertThat(mapStr, equalTo("1=2#3=4"));
    

    2. Spliter

    spliter工作和joiner工作相反,将字符串按照指定的字符进行分割,

    //不忽略空值的分割
    List<String> result = Splitter.on("|").splitToList("1|2");
    assertThat(result, notNullValue());
    assertThat(result.size(), equalTo(2));
    assertThat(result.get(0), equalTo("1"));
    assertThat(result.get(1), equalTo("2"));
    
    //忽略空值的语法 omitEmptyStrings,
    List<String> result = Splitter.on("|").splitToList("1|2|||");
    assertThat(result, notNullValue());
    assertThat(result.size(), equalTo(5));
    result = Splitter.on("|").omitEmptyStrings().splitToList("1|2|||");
    assertThat(result, notNullValue());
    assertThat(result.size(), equalTo(2));
    
    //对切分后的每个结果进行trim两边的空格,语法,trimResults
    List<String> result = Splitter.on("|").omitEmptyStrings().splitToList("1 | 2|||");
    assertThat(result, notNullValue());
    assertThat(result.size(), equalTo(2));
    assertThat(result.get(0), equalTo("1 "));
    assertThat(result.get(1), equalTo(" 2"));
    result = Splitter.on("|").trimResults().omitEmptyStrings().splitToList("1 | 2|||");
    assertThat(result.get(0), equalTo("1"));
    assertThat(result.get(1), equalTo("2"));
    
    //按照长度进行切分
    List<String> result = Splitter.fixedLength(4).splitToList("aaaabbbbccccdddd");
    assertThat(result, notNullValue());
    assertThat(result.size(), equalTo(4));
    assertThat(result.get(0), equalTo("aaaa"));
    assertThat(result.get(3), equalTo("dddd"));
    
    //分割之后,限制最大的分割数量
    List<String> result = Splitter.on("#").limit(3).splitToList("1#2#3#4#5");
    assertThat(result, notNullValue());
    assertThat(result.size(), equalTo(3));
    assertThat(result.get(0), equalTo("1"));
    assertThat(result.get(1), equalTo("2"));
    assertThat(result.get(2), equalTo("3#4#5"));
    
    //正则切分
    List<String> result = Splitter.on(Pattern.compile("\\|")).trimResults().omitEmptyStrings().splitToList("1| 2|||");
    assertThat(result, notNullValue());
    assertThat(result.size(), equalTo(2));
    assertThat(result.get(0), equalTo("1"));
    assertThat(result.get(1), equalTo("2"));
    
    //切分成Map
    Map<String, String> result = Splitter.on(Pattern.compile("\\|")).trimResults()
                                                 .omitEmptyStrings().withKeyValueSeparator("=").split("1=2| 3=4|||");
    assertThat(result, notNullValue());
    assertThat(result.size(), equalTo(2));
    assertThat(result.get("1"),equalTo("2"));
    assertThat(result.get("3"),equalTo("4"));
    

    相关文章

      网友评论

          本文标题:002 Guava | Utilities

          本文链接:https://www.haomeiwen.com/subject/tqrfuqtx.html