Google的Guava工具类splitter和apache stringutil对比 编辑
一直用的是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对比 编辑的更多相关文章
- 强大的 Guava 工具类
Java 开发的同学应该都使用或者听说过 Google 提供的 Guava 工具包.日常使用最多的肯定是集合相关的工具类,还有 Guava cache,除了这些之外 Guava 还提供了很多有用的功能 ...
- 工具篇:介绍几个好用的guava工具类
前言 平时我们都会封装一些处理缓存或其他的小工具.但每个人都封装一次,重复造轮子,有点费时间.有没有一些好的工具库推荐-guava.guava是谷歌基于java封装好的开源库,它的性能.实用性,比我们 ...
- Google的java工具类Guava
前言 google开发java项目肯定也不想重复造轮子,所以肯定也有工具类,就是它了:Guava 我将举例几个实际的例子,发挥这个工具类好用的功能.更多的方法和功能,还有内部的实现可以直接参考http ...
- Google guava工具类的介绍和使用
概述 工具类 就是封装平常用的方法,不需要你重复造轮子,节省开发人员时间,提高工作效率.谷歌作为大公司,当然会从日常的工作中提取中很多高效率的方法出来.所以就诞生了guava.. 高效设计良好的API ...
- Guava工具类
原文链接:http://blog.csdn.net/mnmlist/article/details/53425865 Objects类 Objects类有几个比较不错的方法,toString.hash ...
- 【java】java工具类StringUtils,org.apache.commons.lang3.StringUtils
使用过程中,发现StringUtils工具类功能非常的多. 例如,判断元素是否为数字: StringUtils.isNumeric(string)
- Google Guava学习笔记——基础工具类Splitter的使用
另一项经常对字符串的操作就是根据指定的分隔符对字符串进行分隔.我们基本上会使用String.split方法: String testString = "Monday,Tuesday,,Thu ...
- Guava 工具类之 Splitter的使用
Splitter可以对字符串进行分割,在分割时的方式有2种, 1.按字符/字符串分割 2.按正则进行分割 Splitter在分割完成时可以转换成list和map 一.按字符进行分割 //1.用指定字符 ...
- Guava 工具类之Cache的使用
一.guava cache 介绍 1.介绍 guava cache是Google guava中提供的一款轻量级的本地缓存组件,其特点是简单.轻便.完善.扩展性强,内存管理机制也相对完善. 2.使用缓存 ...
随机推荐
- Linux_install mod_ssl openssl apache
1.下载 mod_ssl 和 apache 登入http://www.modssl.org/source/,下载 mod_ssl-2.8.31-1.3.41.targz: 2.8.31是mod_ssl ...
- 转:Red Hat JBoss团队发布WildFly 8,全面支持Java EE 7并包含全新的嵌入式Web服务器
原文来自于:http://www.infoq.com/cn/news/2014/02/wildfly8-launch Red Hat的JBoss部门今天宣布WildFly 8正式发布.其前身是JBos ...
- sqoop组件运行出错问题解决--com.mysql.jdbc.Driver
sqoop list-tables --connect jdbc:mysql://192.168.11.94:3306/huochetoudalian --username xxx -password ...
- layer iframe层的使用,传参
父层 <div class="col-xs-4 text-left" style="padding-left: 50px;"><button ...
- HDU_2054——A=B问题
Problem Description Give you two numbers A and B, if A is equal to B, you should print "YES&quo ...
- HDU_2029——回文串的判断
Problem Description “回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串.请写一个程序判断读入的字符串是否是“回文”. Input 输入包 ...
- Binary Tree Zigzag Level Order Traversal (LeetCode) 层序遍历二叉树
题目描述: Binary Tree Zigzag Level Order Traversal AC Rate: 399/1474 My Submissions Given a binary tree, ...
- 解析Xcode把应用程序打包成ipa---解决打包完新版本itunes提示不是有效应用程序的问题
Xcode把应用程序打包成ipa是本文要介绍的内容,不多说,先俩看内容.注意:本方法需要先制作假凭证编译于项目中,否则产生的ipa还是无法于iPhone中运行. 制作方法请参考: http://blo ...
- 《UNIX环境高级编程》笔记--信号集
1.信号集基本操作 我们需要有一个能表示多个信号--信号集(signal set)的数据类型.POSIX.1定义了数据类型sigset_t以包含一个信号 集,并且定义了一下五个处理信号处理信号集函数. ...
- JavaScript 自动分页插件 datatables
DataTables Table plug-in for jQuery https://www.datatables.net/