Guava学习

作者: wanggs | 来源:发表于2017-09-30 17:36 被阅读10次

Guava学习笔记之Joiner ,Strings,Splitter 工具实例

一.Strings

package com.spring_boot.guava.strings;

import com.google.common.base.Strings;
import org.junit.Test;

/**
 * Created by wanggs on 2017/9/30.
 * 一.Strings
 */
public class StringsTest {
    @Test
    public void right() {
        String str = "abcd";
        String rstr = Strings.padEnd(str, 10, 'x');
        System.out.println(rstr);
        // 结果为:abcdxxxxxx
    }

    @Test
    public void left() {
        String str = "abcd";
        String rstr = Strings.padStart(str, 10, 'x');
        System.out.println(rstr);
        // 结果为:xxxxxxabcd
    }

    @Test
    public void isNullOrEmpty() {
        String str = "";
        boolean result = Strings.isNullOrEmpty(str);
        System.out.println(result);

        // 结果为: true
    }

    @Test
    public void nullToEmpty() {
        //Strings.nullToEmpty  如果为null 转为""
        String str = null;
        String result = Strings.nullToEmpty(str);
        System.out.println(result);
        // 结果为:
    }

    @Test
    public void emptyToNull() {
        //Strings.emptyToNull  如果为"" 转为null
        String str = "";
        String result = Strings.emptyToNull(str);
        System.out.println(result);
        // 结果为:null
    }

    @Test
    public void repeat() {
        String str = "123";
        String result = Strings.repeat(str, 3);
        System.out.println(result);
        // 结果为: 123123123
    }

    @Test
    public void commonPrefix() {
        //Strings.commonPrefix 获取a,b左公共部分字符串
        String a = "abcdsfsfs";
        String b = "abc3sfsd";
        String c = Strings.commonPrefix(a, b);
        System.out.println(c);
        // 结果为:abc
    }

    @Test
    public void commonSuffix() {
       //Strings.commonSuffix 获取a,b右公共部分字符串
        String a = "faaxyz";
        String b = "fwefxyz";
        String c = Strings.commonSuffix(a, b);
        System.out.println(c);
        // 结果为:xyz
    }
}

二,Joiner 连接工具类

package com.spring_boot.guava.Joiner;

import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import org.junit.Test;

import java.util.Iterator;
import java.util.List;

/**
 * Created by wanggs on 2017/9/30.
 * 二,Joiner 连接工具类
 * <p>
 * 分析源码可知:该类构造方法被private修身,无法直接通过new实现
 * 通过调用on传如分隔符来得到实例。
 */
public class JoinerTest {
    @Test
    public void list() {
        // 1.连接list列表
        Joiner joiner = Joiner.on(",");
        List<String> list = Lists.newArrayList("a", "b", "c");
        System.out.println(joiner.join(list));
        // 结果为: a,b,c
    }

    @Test
    public void iterator() {
        // 2.连接Iterator<T>列表
        Joiner joiner = Joiner.on(",");
        List<String> list = Lists.newArrayList("a", "b", "c");
        Iterator<String> iterator = list.iterator();
        System.out.println(joiner.join(iterator));
       // 结果为:a,b,c
    }

    @Test
    public void strings(){
        // 3.连接多个字符串
        Joiner joiner =  Joiner.on(",");
        System.out.println(joiner.join("123","345","56","43"));
       // 结果:123,345,56,43
    }
    @Test
    public void listString(){
        // 4.连接字符串与列表
        Joiner joiner = Joiner.on(",");
        List<String> list = Lists.newArrayList("a", "b", "c");
        StringBuilder builder=new StringBuilder("标题");
        System.out.println(joiner.appendTo(builder,list));
        // 结果:标题a,b,c
    }
    @Test
    public void jumpnull(){
        Joiner joiner =  Joiner.on(",").skipNulls();
        List<String> list = Lists.newArrayList("a", null,"b", "c");
        StringBuilder builder=new StringBuilder("标题");
        System.out.println(joiner.appendTo(builder,list));
        // 结果:标题a,b,c
    }
    @Test
    public void replace(){
        Joiner joiner =  Joiner.on(",").useForNull("123");//替换null为123
        List<String> list = Lists.newArrayList("a", null,"b", "c");
        StringBuilder builder=new StringBuilder("标题");
        System.out.println(joiner.appendTo(builder,list));
        // 结果:标题a,123,b,c
    }
}

三.Splitter 工具类

package com.spring_boot.guava.splitter;

import com.google.common.base.Splitter;
import com.google.common.escape.Escaper;
import com.google.common.html.HtmlEscapers;
import org.junit.Test;

import java.util.List;

/**
 * Created by wanggs on 2017/9/30.
 * 主要功能是拆分字符串为集合 Map等。
 */
public class SplitterTest {
    @Test
    public void splitterList() {
        Splitter splitter = Splitter.on(",");
        String str = "a,b,c,d,e,f,g";
        List<String> list = splitter.splitToList(str);
        System.out.println(list);
        // 结果:[a, b, c, d, e, f, g]
    }

    @Test
    public void ignore() {
        Splitter splitter = Splitter.on(",").omitEmptyStrings();
        String str = "a,b,c,d,,f,g";
        List<String> list = splitter.splitToList(str);
        System.out.println(list);
        //  结果:[a, b, c, d, f, g]
    }

    @Test
    public void ignoreSp() {
        Splitter splitter = Splitter.on(",").omitEmptyStrings().trimResults();
        String str = "a,b,c,d,,f,  g  ";
        List<String> list = splitter.splitToList(str);
        System.out.println(list);

        // 结果:[a, b, c, d, f, g]
    }

    @Test
    public void htmlEscapers() {

        Escaper htmlEscaper = HtmlEscapers.htmlEscaper();
        System.out.println(htmlEscaper.escape("<a>你好</a>"));

        // 结果:<a>你好</a>
    }
}

相关文章

  • Guava 学习

    guava Splitter 学习 guava Sets 集合类取交集、差集、并集

  • 1、guava简介

    转载:Guava学习笔记:Google Guava 类库简介 Guava项目包含我们在基于Java的项目中依赖的几...

  • Guava学习笔记:Optional优雅的使用null

    Guava学习笔记:Optional优雅的使用null 在我们学习和使用Guava的Optional之前,我们需要...

  • Guava

    1.Guava学习笔记:Google Guava 类库简介[http://www.cnblogs.com/peid...

  • Guava学习

    Google生产的工具类,目的是简化开发 Optional - 用来表示可能为null的T类型引用(增强null的...

  • Guava学习

    通过使用guava库,让代码简洁易扩展。 1、条件检查 业务代码书写过程中,各种判空和参数检查是不可避免的,重复繁...

  • Guava学习

    Guava学习笔记之Joiner ,Strings,Splitter 工具实例 一.Strings 二,Joine...

  • Guava学习

    字符串处理 Joiner是连接器,Splitter是分割器,通常我们会把它们定义为static final,利用o...

  • Guava -- 集合类 和 Guava Cache

    Guava -- 集合类 和 Guava Caches 1. 什么是 Guava Guava 是 google ...

  • guava 限流RateLimiter 初探(一)

    Guava RateLimiter Guava RateLimiter原理 Guava RateLimiter基于...

网友评论

    本文标题:Guava学习

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