为了账号安全,请及时绑定邮箱和手机立即绑定

Google Guava之字符串连接与分割

标签:
Java
字符串连接 -- Joiner

Joiner.on(separator).join(parts)

返回值为String。执行join()之前,必须使用on()设置分隔符。
parts支持IterableIteratoObject[]以及不常用的String型不定参。

代码清单1——数组

String[] numberArray = new String[] { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
                "Ten" };
String numbers = Joiner.on(";").join(numberArray);
System.out.println(numbers);
//输出:One;Two;Three;Four;Five;Six;Seven;Eight;Nine;Ten

代码清单2——Iterable

Iterable<String> numberIter = Splitter.on(",").split("One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten");
String numbers = Joiner.on(";").join(numberIter);
System.out.println(numbers);

//输出:One;Two;Three;Four;Five;Six;Seven;Eight;Nine;Ten

代码清单3——String不定参

String numbers = Joiner.on(";").join("One", "Two", "Three", "Four");
System.out.println(numbers);
//输出:One;Two;Three;Four

代码清单4——Iterator

String[] numberArray = new String[] { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
                "Ten" };
Iterator<String> iterators = Arrays.asList(numberArray).iterator();
String numbers = Joiner.on(";").join(iterators);
System.out.println(numbers);
//输出 One;Two;Three;Four;Five;Six;Seven;Eight;Nine;Ten

Joiner.on(separator).skipNulls().join(parts)

使用skipNulls(),可以忽略null值。避免parts中有null值时,抛出NPE异常。

代码清单5

String numbers = Joiner.on(";").join("One","Two","Three",null,"Five");
System.out.println(numbers);
// 抛出异常 java.lang.NullPointerException

代码清单6

String numbers = Joiner.on(";").skipNulls().join("One","Two","Three",null,"Five");
System.out.println(numbers);
//输出:One;Two;Three;Five

Joiner.on(separator).useForNull(nullText).join(parts)

使用useForNull(),可以将part中的null值替换为指定字符。

代码清单7

String numbers = Joiner.on(";").useForNull("NULL").join("One", "Two", "Three", null, "Five");
System.out.println(numbers);
//输出:One;Two;Three;NULL;Five

字符串分割 -- Splitter

Splitter.on(separator).split(sequence)

返回值为Iterable。执行split()前,必须使用on()设置分隔符。
从代码清单8的输出结果可以看出,分割结果并未过滤掉空字符串。

代码清单8

String numberList = "One,Two,Three,Four,Five,,Six,Seven,Eight,Nine,Ten,";
Iterable<String> split = Splitter.on(",").split(numberList);
for (String str : split) {
    System.out.print(str + ";");
}

//输出:One;Two;Three;Four;Five;;Six;Seven;Eight;Nine;Ten;;

Splitter.on(separator).omitEmptyStrings().split(sequence)

通过使用omitEmptyStrings(),设置Splitter过滤掉length=0的空字符串

代码清单9

String numberList = "One,Two,Three,Four,Five,,Six,Seven,Eight,Nine,Ten,";

Iterable<String> split = Splitter.on(",").omitEmptyStrings().split(numberList);
for (String str : split) {
    System.out.print(str + ";");
}

//输出One;Two;Three;Four;Five;Six;Seven;Eight;Nine;Ten;

字符串分割效率测试

常用字符串分割方法或工具类有:

  • JDK : String自带方法
  • commons-lang3 : StringUtils工具类
  • Google Guave : Splitter工具类

测试代码

/**
     * 使用JDK自带方法
     */
    public void splitByJdk() {
        String numberList = "One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten";
        long start = System.currentTimeMillis();
        for (int i = 0; i < 2000000; i++) {
            numberList.split(",");
        }
        long end = System.currentTimeMillis();
        System.out.println("JDK耗时:" + (end - start));
    }
    /**
     * 使用commons-lang3
     */
    public void splitByCommons() {
        String numberList = "One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten";
        long start = System.currentTimeMillis();
        for (int i = 0; i < 2000000; i++) {
            StringUtils.split(numberList, ",");
        }
        long end = System.currentTimeMillis();
        System.out.println("commons耗时:" + (end - start));
    }
    /**
     * 使用Google Guava
     */
    public void splitByGuava() {
        String numberList = "One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten";
        long start = System.currentTimeMillis();
        for (int i = 0; i < 2000000; i++) {
            Splitter.on(",").omitEmptyStrings().split(numberList);
        }
        long end = System.currentTimeMillis();
        System.out.println("guava耗时:" + (end - start));
    }
    /**
     * 使用Google Guava
     * 循环外初始化Splitter对象,减少声明
     */
    public void splitByGuava2() {
        String numberList = "One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten";
        long start = System.currentTimeMillis();
        Splitter on = Splitter.on(",").omitEmptyStrings();
        for (int i = 0; i < 2000000; i++) {
            on.split(numberList);
        }
        long end = System.currentTimeMillis();
        System.out.println("guava2耗时:" + (end - start));
    }

测试结果

图片描述

点击查看更多内容
3人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
26
获赞与收藏
15

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消