Google Guava工具类详解


同样写给自己最好别忘了工具类的积累对我们日常工作中很重要,所以记录一下

public class JoinTest {
    final List list = Arrays.asList("Hegao", "Guava", "Shibai");
    final List listandNull = Arrays.asList("Hegao", "Guava", "Shibai", null);
    final String fileName = "D:\\资料\\test.txt";
    final Map, String> stringStringMap = ImmutableMap.of("Guava", "Map", "Test", "HAH");

    /**
     * Hegao#Guava#Shibai#HAHHA
     */
    @Test
    public void JoinTest() {
        String join = Joiner.on("#").join(list);
        System.out.println(join);
    }

    /**
     * Hegao#Guava#Shibai
     */
    @Test
    public void JoinTestNotNull() {
        String join = Joiner.on("#").skipNulls().join(listandNull);
        System.out.println(join);
    }

    /**
     * Hegao#Guava#Shibai#HAHHA
     */
    @Test
    public void JoinTestNotNullWithDefaultValue() {
        String join = Joiner.on("#").useForNull("HAHHA").join(listandNull);
        System.out.println(join);
    }

    /**
     * Hegao*Guava*Shibai
     *
     * @throws IOException
     */
    @Test
    public void JoinTest_JoinOnAppend_StringBudiful() throws IOException {
        StringBuffer buffer = Joiner.on("*").skipNulls().appendTo(new StringBuffer(), listandNull);
        System.out.println(buffer);
    }

    /**
     * Hegao*Guava*Shibai*DEFAULT 吧空格变成DEFAULT
     * result Hegao*Guava*Shibai*DeFault
     *
     * @throws IOException
     */
    @Test
    public void JoinTest_JoinOnAppend_StringBudiful_append_writer() throws IOException {
        try (FileWriter writer = new FileWriter(new File(fileName))) {
            FileWriter to = Joiner.on("*").useForNull("DeFault").appendTo(writer, listandNull);
        } catch (IOException o) {
            System.out.println(o);
        }
    }

    /**
     * java 8 的写法跟上面一样
     *
     * @throws IOException
     */
    @Test
    public void TestJoinBystream() throws IOException {
        String s = listandNull.stream().filter(i -> i != null && !i.isEmpty()).collect(joining("#"));
        System.out.println(s);
    }

    /**
     * Default的用法
     *
     * @throws IOException
     */
    @Test
    public void StreamTest() throws IOException {
        String s = listandNull.stream().map(this::defaultvalue).collect(joining("*"));
        System.out.println(s);
    }

    private String defaultvalue(final String item) {
        return item == null || item.isEmpty() ? "DEFAULT" : item;
    }

    /**
     * Guava=Map,Test=HAH map变成string
     */
    @Test
    public void JoinOnWitgMap() {
        String join = Joiner.on(",").withKeyValueSeparator("=").join(stringStringMap);
        System.out.println(join);
    }

    /**
     * map变为stringe
     */
    @Test
    public void JoinOnWitgMapToAppend() {
        String join = Joiner.on(",").withKeyValueSeparator("=").join(stringStringMap);
        System.out.println(join);
    }
 
  

    /**
     * 拆分result=Hello  world
     */
    @Test
    public void spitinlist() {
        List list = Splitter.on("|").splitToList("Hello|world");
        list.stream().forEach(System.out::println);
    }

    /**
     * 去掉空格
     */
    @Test
    public void spiti_onspit_OmitEpty() {
        List list = Splitter.on("|").omitEmptyStrings().splitToList("Hello | world|||");
        list.stream().forEach(System.out::println);
        System.out.println(list.size());
        //把前后的空格去tremendous        List split = Splitter.on("|").omitEmptyStrings().trimResults().splitToList("Hello | world|||");
        split.stream().forEach(System.out::println);
        System.out.println(split.size());
    }

    /**
     * aaaabbbbccccdddd  相当于报文 result:[aaaa, bbbb, cccc, dddd]
     */
    @Test
    public void test_splt_FixLEngth() {
        List result = Splitter.fixedLength(4).splitToList("aaaabbbbccccdddd");
        System.out.println(result);
        System.out.println(result.size());
    }

    /**
     * 取前3result 3
     */
    @Test
    public void test_splt_FixLEngth_limit() {
        List list = Splitter.on("#").limit(3).splitToList("hello#world#heh#ss");
        System.out.println(list.size());
    }

    /**
     * 正则表达式正则的表达式
     * [Hello, world]
     */
    @Test
    public void splt_on_patterstring() {
        List list = Splitter.onPattern("\\|").trimResults().omitEmptyStrings().splitToList("Hello | world|||");
        System.out.println(list.size());
        System.out.println(list);
    }

    /**
     * 也就是说吧list \\| 去切个 到map里面去
     */
    @Test
    public void splt_on_patterstring_to_map() {
        Map, String> map = Splitter.onPattern("\\|").
                trimResults().omitEmptyStrings().
                //key value 是通过什么风格的
                        withKeyValueSeparator("=").split("Hello=HELLO | world=WOLD|||");

        System.out.println(map.get("Hello"));
        System.out.println(map.get("world"));
        //先拆分在
        String toSpilt = "a=b,b=c,d=e";
        Map, String> mp = Splitter.onPattern("[,;]{1,}").withKeyValueSeparator("=").split(toSpilt);
        for (Map.Entry, String> entry : mp.entrySet()) {
            System.out.println(String.format("%s=%s", entry.getKey(), entry.getValue()));
        }
    }


你可能感兴趣的:(JAVA,Guava)