Guava常用方法

def "获取字符在一个字符串中出现的次数"() {
    given:
    def a = "日子" //查询 '日' 和 '子' 在字符串中出现的次数
    def b = "假如生活欺骗了你,\n" +
            "不要悲伤,不要心急!\n" +
            "忧郁的日子里须要镇静:\n" +
            "相信吧,快乐的日子将会来临!\n" +
            "心儿永远向往着未来;\n" +
            "现在却常是忧郁。\n" +
            "一切都是瞬息,一切都将会过去;\n" +
            "而那过去了的,就会成为亲切的怀恋。"
    println CharMatcher.anyOf(a).countIn(b);//4
}




def "校验两个汉字是否相等(不分先后顺序)"() {
    given:
 
    def name = "cuiyongxu"
    def name2 = "yongxucui"
    when:
    byte[] b1 = name.getBytes();
    byte[] b2 = name2.getBytes();
 
    byte[] b3 = name.getBytes(Charsets.US_ASCII);
    byte[] b4 = name2.getBytes(Charsets.US_ASCII);
 
    then:
    false == Arrays.equals(b1, b2)//java 默认  false
    true == Arrays.equals(b3, b4)//true
}



def "拼接字符串"() {
    given:
    Joiner joiner = Joiner.on(":").skipNulls();
    println joiner.join("123", null, "foo", "bar"," ","ddd","","aa",null,"bb");//123:foo:bar: :ddd::aa:bb
}


def "map设置返回默认值,只对null有效,且前后值为同类型"() {
 
    given:
    def a = null
    def b = Lists.newArrayList("aaa")
    when:
    def v = MoreObjects.firstNonNull(a, b)
    then:
    println v //[aaa]
    v instanceof List
}



def "判断字符串是否为空"() {
    given:
    def a = "";
    def b = null;
    def c = "null";
    when:
    def av = Strings.isNullOrEmpty(a)
    def bv = Strings.isNullOrEmpty(b);
    def cv = Strings.isNullOrEmpty(c);
    then:
    true == av
    true == bv
    false == cv
}


def "判断两个值是否相等,可判断任意对象"() {
    given:
    def a = Lists.newArrayList("a")
    def b = Lists.newArrayList("b")
    def c = Lists.newArrayList("a")
    println Objects.equal(a,b)
    println Objects.equal(a,c)
}


def "判断参数是否符合某种条件,不推荐使用,但是可模拟写法"() {
    given:
    def age = 55
    boolean flag = age >= 18 && age < 99;
    Preconditions.checkArgument(flag, "age must in range (18,99)");
}


def "将String转换成list"() {
    given:
    String string = "2,a,c,5,e,6";
    when:
    List<String> list = Splitter.on(",").splitToList(string)
    then:
    println list
}


def "将String转换成map"() {
    given:
    String string = "wansong=万*,liyiguang=李*光,wangzhuang=王*,lichaoshuai=李*帅";
    when:
    Map<String, String> map = Splitter.on(",").withKeyValueSeparator("=").split(string);
    then:
    println map.get("cuiyongxu")
}


def "将String转换成list,去空格"() {
    given:
    String string = "2,a , c  ,5  ,e,  6";
    when:
    List<String> list = Splitter.on(",").omitEmptyStrings().trimResults().splitToList(string)
    then:
    println list
}


def "Map转String"() {
    given:
    Map<String, String> map = Maps.newHashMap()
    map.put("a", "b");
    map.put("c", "d");
    println Joiner.on(",").withKeyValueSeparator("=").join(map);
}


def "将String按照特定规则切割,不去空值"() {
    given:
    String string = "aa.dd,,ff,,.";
    List<String> list = Splitter.onPattern("[.|,]").splitToList(string);
    println list
}


def "正则分割,可包含多个标点符号,去空格,并转list"() {
    given:
    String string = "aa.dd,,ff,,.";
    List<String> list = Splitter.onPattern("[.|,]").omitEmptyStrings().splitToList(string);
    println list
}



def "正则分割,可包含多个标点符号,去空格"() {
    given:
    Iterable<String> splitResults = Splitter.onPattern("[,,]{1,}")
            .trimResults()
            .omitEmptyStrings()
            .split("hello,word,,世界,水平, , , 我的,, 你的,大家的,");
 
    splitResults.forEach({
        String str ->
            str;
            println str
    })
}



def "获得两个字符串相同的前缀"() {
    given:
    def a = "com.google.common.base";
    def b = "com.google.common.net";
    println Strings.commonPrefix(a, b);
}

 

您还没有登录,请先 登录或者 注册后,添加评论