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.使用缓存 ...
随机推荐
- Python自动化运维之10、模块之json、pickle、XML、PyYAML、configparser、shutil
序列化 Python中用于序列化的两个模块 json 用于[字符串]和 [python基本数据类型] 间进行转换 pickle 用于[python特有的类型] 和 [python基本数据类 ...
- strnclmp和strlen函数的用法
一.strncmp 函数 函数原型: 1.函数原型:int strncmp (const char *s1, const char *s2, size_t n) 2.头文件: <string. ...
- POI操作Excel2007实例二之“SXSSFWorkbook”处理海量数据
转自:http://blog.csdn.net/little_stars/article/details/8266262 前文讲述了 POI 读取的基本操作,但后期 经过试验,当写入数据量超过5万条以 ...
- Storm drpc学习
示例代码: package com.lky.test; import org.apache.commons.logging.Log; import org.apache.commons.logging ...
- Truncate Table 用法
TRUNCATE TABLE 删除表中的所有行,而不记录单个行删除操作. 语法 TRUNCATE TABLE name 参数 name 是要截断的表的名称或要删除其全部行的表的名称. 注释 TRUNC ...
- iOS--tableview分组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ ; } -(NSInteger)tableView:(UITable ...
- JavaScript 自动分页插件 datatables
DataTables Table plug-in for jQuery https://www.datatables.net/
- Javascript:DOM表格操作
需求说明: /* *需求说明: *获取元素:tBodies,tHead,tFoot,rows,cells *表格的创建 *数据添加 *隔行变色 *删除操作,剩余表格重新计算,实现隔行变色 */ HTM ...
- uploadify不能正确显示中文的按钮文本的解决办法
uploadify 目前不能正确显示中文的按钮文本. 我发现bug的原因是uploadify错误的使用了 js 的 escape 和 flash 的 unescape配对,而这2个是不兼容的.正确的转 ...
- 10个热门IT证书
MCP (微软专家认证) CCNA (思科认证网络支持工程师) MCPD (微软认证开发专家) SCJP (SUN认证Java程序员) CISSP (信息系统安全认证专家) CompTIA A+认证 ...