google的guava工具类splitter和apache stringutil对比
2013-02-25 20:11
295 查看
一直用的是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 niceCommaSplitter = Splitter.on(',')
.omitEmptyString()
.trimResults();
这里去掉多余的空格,调用时,比如
niceCommaSplitter.split('one,, two, three'); //'one','two','three'
niceCommaSplitter.split(' four , five '); //'four','five'
这个则比较方便。要注意的是splitter返回的是Iterable<String>,这个和StringUtil
有点不同。
效率方面的对比,作者作了比较:
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201107/e231cad9113e50ffb61a667acf05640c.gif)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201506/c3f713bc49f7d3c749d0ee3bca7960fe.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201302/dc190ccff41598e6840b8bcc41f028fd.gif)
final String numberList = 'One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten';
long start = System.currentTimeMillis();
for(int i=0; i<1000000; i++) {
StringUtils.split(numberList , ',');
}
System.out.println(System.currentTimeMillis() - start);
start = System.currentTimeMillis();
for(int i=0; i<1000000; i++) {
Splitter.on(',').split(numberList );
}
System.out.println(System.currentTimeMillis() - start);
结果发现:
594
31
splitter快10倍以上!主要是怀疑因为splitter返回的是Iterable<String>,不用每次new String对象。
再来一个测试
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201107/e231cad9113e50ffb61a667acf05640c.gif)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201506/c3f713bc49f7d3c749d0ee3bca7960fe.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201302/dc190ccff41598e6840b8bcc41f028fd.gif)
final String numberList = 'One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten';
long start = System.currentTimeMillis();
for(int i=0; i<1000000; i++) {
final String[] numbers = StringUtils.split(numberList, ',');
for(String number : numbers) {
number.length();
}
}
System.out.println(System.currentTimeMillis() - start);
Splitter splitter = Splitter.on(',');
start = System.currentTimeMillis();
for(int i=0; i<1000000; i++) {
Iterable<String> numbers = splitter.split(numberList);
for(String number : numbers) {
number.length();
}
}
System.out.println(System.currentTimeMillis() - start);
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 niceCommaSplitter = Splitter.on(',')
.omitEmptyString()
.trimResults();
这里去掉多余的空格,调用时,比如
niceCommaSplitter.split('one,, two, three'); //'one','two','three'
niceCommaSplitter.split(' four , five '); //'four','five'
这个则比较方便。要注意的是splitter返回的是Iterable<String>,这个和StringUtil
有点不同。
效率方面的对比,作者作了比较:
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201107/e231cad9113e50ffb61a667acf05640c.gif)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201506/c3f713bc49f7d3c749d0ee3bca7960fe.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201302/dc190ccff41598e6840b8bcc41f028fd.gif)
final String numberList = 'One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten';
long start = System.currentTimeMillis();
for(int i=0; i<1000000; i++) {
StringUtils.split(numberList , ',');
}
System.out.println(System.currentTimeMillis() - start);
start = System.currentTimeMillis();
for(int i=0; i<1000000; i++) {
Splitter.on(',').split(numberList );
}
System.out.println(System.currentTimeMillis() - start);
final String numberList = 'One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten'; long start = System.currentTimeMillis(); for(int i=0; i<1000000; i++) { StringUtils.split(numberList , ','); } System.out.println(System.currentTimeMillis() - start); start = System.currentTimeMillis(); for(int i=0; i<1000000; i++) { Splitter.on(',').split(numberList ); } System.out.println(System.currentTimeMillis() - start);
结果发现:
594
31
splitter快10倍以上!主要是怀疑因为splitter返回的是Iterable<String>,不用每次new String对象。
再来一个测试
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201107/e231cad9113e50ffb61a667acf05640c.gif)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201506/c3f713bc49f7d3c749d0ee3bca7960fe.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201302/dc190ccff41598e6840b8bcc41f028fd.gif)
final String numberList = 'One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten';
long start = System.currentTimeMillis();
for(int i=0; i<1000000; i++) {
final String[] numbers = StringUtils.split(numberList, ',');
for(String number : numbers) {
number.length();
}
}
System.out.println(System.currentTimeMillis() - start);
Splitter splitter = Splitter.on(',');
start = System.currentTimeMillis();
for(int i=0; i<1000000; i++) {
Iterable<String> numbers = splitter.split(numberList);
for(String number : numbers) {
number.length();
}
}
System.out.println(System.currentTimeMillis() - start);
final String numberList = 'One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten'; long start = System.currentTimeMillis(); for(int i=0; i<1000000; i++) { final String[] numbers = StringUtils.split(numberList, ','); for(String number : numbers) { number.length(); } } System.out.println(System.currentTimeMillis() - start); Splitter splitter = Splitter.on(','); start = System.currentTimeMillis(); for(int i=0; i<1000000; i++) { Iterable<String> numbers = splitter.split(numberList); for(String number : numbers) { number.length(); } } System.out.println(System.currentTimeMillis() - start);
相关文章推荐
- Google的Guava工具类splitter和apache stringutil对比 编辑
- Google Guava学习笔记——基础工具类Splitter的使用
- Google Guava Splitter
- google工具类guava
- Google Guava学习笔记——基础工具类Joiner的使用
- [Google Guava] 2.3-强大的集合工具类:java.util.Collections中未包含的集合工具
- [Google Guava] 2.3-强大的集合工具类:java.util.Collections中未包含的集合工具
- [Google Guava] 2.4-集合扩展工具类
- Google Guava vs Apache Commons for Argument Validation
- Google Guava的splitter用法
- Google Guava 库 CharMatcher 、Joiner以及Splitter
- [Google Guava] 2.4-集合扩展工具类
- [Google Guava] 2.3-强大的集合工具类:java.util.Collections中未包含的集合工具
- 134_容器_git工具_Google_guava与Apache_commons的准备工作(jar src doc)
- Google Guava学习笔记——基础工具类针对Object类的使用
- Google guava工具类的介绍和使用
- Google guava工具类的介绍和使用
- Java-No.14 Google-Guava工具类整理
- Google guava工具类的介绍和使用
- Apache Comons+Google Guava