一直用的是apache的stringutil工具类,其实google的工具类项目 guava中居然也有字符串的分隔类splitter的,在 http://code.google.com/p/guava-libraries/中可以下载,其中在老外的 http://www.javacodegeeks.com/2012/12/guava-splitter-vs-stringutils.html 这篇文章中进行了stringutil的对比:

首先看两者的用法:

    // Apache StringUtils...
String[] tokens1 = StringUtils.split("one,two,three",','); // Guava splitter...
Iterable<String> tokens2 = Splitter.on(',').split("one,two,three");

 StringUtils静态类来的,spiltter的语法中则要new对象,但splitter中,一个优点
是,可以去掉多余的空格等,比如:

    Splitter splitter = Splitter.on(',').omitEmptyStrings().trimResults();  

            Iterable<String> tokens3 = splitter.split("one,,two,three");
Iterator<String> iterator = tokens3.iterator();
while (iterator.hasNext()) {
String value = iterator.next();
System.out.println(value);
}

输出结果是:.

    one
two
three

这个则比较方便。要注意的是splitter返回的是Iterable<String>,这个和StringUtil
有点不同。

效率方面的对比,作者作了比较:

    final String numberList = "One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten";
long startTime = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
StringUtils.split(numberList,',');
}
System.out.print("StringUtils split expenditure time : ");
System.out.println(System.currentTimeMillis() - startTime); startTime = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
Splitter.on(',').split(numberList);
}
System.out.print("Splitter split expenditure time : ");
System.out.println(System.currentTimeMillis() - startTime);

输出结果:

    StringUtils split expenditure time : 672
Splitter split expenditure time : 312

splitter快上!主要是怀疑因为splitter返回的是Iterable<String>,不用每次new String对象。

再来一个测试:

    final String numberList = "One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten";
long startTime = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
final String[] numbers = StringUtils.split(numberList,',');
for (String number : numbers) {
number.length();
}
}
System.out.print("StringUtils split expenditure time : ");
System.out.println(System.currentTimeMillis() - startTime); Splitter splitter = Splitter.on(',');
startTime = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
Iterable<String> numbers = splitter.split(numberList);
for (String number : numbers) {
number.length();
}
}
System.out.print("Splitter split expenditure time : ");
System.out.println(System.currentTimeMillis() - startTime);

结果如下:

    StringUtils split expenditure time : 735
Splitter split expenditure time : 2062

spitter这次更慢了! 所以感觉,如果StringUtil够用的话,其实用StringUtil其实很好的拉。

Google的Guava工具类splitter和apache stringutil对比 编辑的更多相关文章

  1. 强大的 Guava 工具类

    Java 开发的同学应该都使用或者听说过 Google 提供的 Guava 工具包.日常使用最多的肯定是集合相关的工具类,还有 Guava cache,除了这些之外 Guava 还提供了很多有用的功能 ...

  2. 工具篇:介绍几个好用的guava工具类

    前言 平时我们都会封装一些处理缓存或其他的小工具.但每个人都封装一次,重复造轮子,有点费时间.有没有一些好的工具库推荐-guava.guava是谷歌基于java封装好的开源库,它的性能.实用性,比我们 ...

  3. Google的java工具类Guava

    前言 google开发java项目肯定也不想重复造轮子,所以肯定也有工具类,就是它了:Guava 我将举例几个实际的例子,发挥这个工具类好用的功能.更多的方法和功能,还有内部的实现可以直接参考http ...

  4. Google guava工具类的介绍和使用

    概述 工具类 就是封装平常用的方法,不需要你重复造轮子,节省开发人员时间,提高工作效率.谷歌作为大公司,当然会从日常的工作中提取中很多高效率的方法出来.所以就诞生了guava.. 高效设计良好的API ...

  5. Guava工具类

    原文链接:http://blog.csdn.net/mnmlist/article/details/53425865 Objects类 Objects类有几个比较不错的方法,toString.hash ...

  6. 【java】java工具类StringUtils,org.apache.commons.lang3.StringUtils

    使用过程中,发现StringUtils工具类功能非常的多. 例如,判断元素是否为数字: StringUtils.isNumeric(string)

  7. Google Guava学习笔记——基础工具类Splitter的使用

    另一项经常对字符串的操作就是根据指定的分隔符对字符串进行分隔.我们基本上会使用String.split方法: String testString = "Monday,Tuesday,,Thu ...

  8. Guava 工具类之 Splitter的使用

    Splitter可以对字符串进行分割,在分割时的方式有2种, 1.按字符/字符串分割 2.按正则进行分割 Splitter在分割完成时可以转换成list和map 一.按字符进行分割 //1.用指定字符 ...

  9. Guava 工具类之Cache的使用

    一.guava cache 介绍 1.介绍 guava cache是Google guava中提供的一款轻量级的本地缓存组件,其特点是简单.轻便.完善.扩展性强,内存管理机制也相对完善. 2.使用缓存 ...

随机推荐

  1. 信息安全实验四:information-security

    title: authentication date: 2016-01-13 14:33:22 categories: information-security tags: authenticatio ...

  2. oldboy第四天学习

    一.感觉上课没有太多的知识.也可以去理解.但是作业太难了... 二.hash() #python里面的哈希类型是在一个程序中不变,如果换了python 哈希是不#一样的. #字典为什么快,因为他把字典 ...

  3. android 模拟微信消息框 BaseAdapter()方法 [2]

    在昨天的微信布局的基础上加内容 http://www.cnblogs.com/Seven-cjy/p/6098024.html 项目下/res/layout下创建一个 listview_layout. ...

  4. 转:内核中的内存申请:kmalloc、vmalloc、kzalloc、kcalloc、get_free_pages

    在内核模块中申请分配内存需要使用内核中的专用API:kmalloc.vmalloc.kzalloc.kcalloc.get_free_pages;当然,设备驱动程序也不例外;对于提供了MMU功能的处理 ...

  5. Baidu Map Web API 案例

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  6. Android的理解

    从组件的角度来考虑 Activity------------------Service-----------------Broadcast Receiver---------------------- ...

  7. PCB走线和过孔的过流能力

    PCB走线的载流能力与以下因素有关:线宽.线厚(铜箔厚度).容许温升.PCB走线越宽,载流能力越大. 近似计算公式: I=KT0.44A0.75 (K为修正系数,一般覆铜线在内层时取0.024,在外层 ...

  8. HDU_2136——最大质因数,素数筛选法

    Problem Description Everybody knows any number can be combined by the prime number. Now, your task i ...

  9. DPDK2.1开发者手册4-7

    Mempool Labrary 一个内存池(memory pool)就是固定大小对象的分配器.在dpdk中,它是通过名字来标示唯一性的,且使用环形队列来保存没有使用的空闲对象.它提供了一些可选项服务例 ...

  10. 将 Web 应用性能提高十倍的10条建议

    提高 web 应用的性能从来没有比现在更重要过.网络经济的比重一直在增长:全球经济超过 5% 的价值是在因特网上产生的(数据参见下面的资料).这个时刻在线的超连接世界意味着用户对其的期望值也处于历史上 ...