Java StringTokenizer分割字符串

时间:2022-09-15 22:15:05

除了String本身的split方法,JDK在很早就提供了StringTokenizer这个类来分割字符串;

不过,由于正则表达式和String本身的功能越来越多,StringTokenizer已经被官方不建议使用了,至于原因,可参考*上的一个问答:

http://*.com/questions/6983856/why-is-stringtokenizer-deprecated

 

不过,它从jdk最初的版本一直存活到现在,总归有它存在的意义与价值,虽不建议使用,但了解总归是要了解的;

首先参考Oracle的官方文档:

http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html

 

其中一个原因或许就是,单从性能上来说,它的性能要优于String的split吧,不过由于正则表达式的强大,导致StringTokenizer的功能方面相对薄弱了些;

public static void main(String[] args) {
Random random
= new Random();
StringBuilder string
= new StringBuilder();
for (int i = 0; i < 10000000; i++) {
string.append(
" " + random.nextInt(1000));
}

long start = System.currentTimeMillis();
testStringTokenizer(string.toString());
long end = System.currentTimeMillis();
System.out.println(
"testStringTokenizer: " + (end - start) + "ms");

start
= System.currentTimeMillis();
testStringSplit(string.toString());
end
= System.currentTimeMillis();
System.out.println(
"testStringSplit: " + (end - start) + "ms");

}

public static String testStringTokenizer(String str) {
StringBuilder st
= new StringBuilder();
StringTokenizer stringTokenizer
= new StringTokenizer(str);
while (stringTokenizer.hasMoreTokens()) {
st.append(stringTokenizer.nextToken());
}
return st.toString();
}

public static String testStringSplit(String str) {
StringBuilder st
= new StringBuilder();
String[] strs
= str.split("\\s");
for (String string2 : strs) {
st.append(string2);
}
return st.toString();
}

对比结果:

testStringTokenizer: 476ms
testStringSplit: 2655ms

从代码层次上来看,的确是StringTokenizer要快些。

参考*上的说法,不过具体详情可参考:https://www.cpe.ku.ac.th/~jim/java-io.html

StringTokenizer is not deprecated in fact StringTokenizer is 4X faster than String.split() and in competitive programming it is used by many developers.

 

参考内容有:

http://blog.csdn.net/songylwq/article/details/9016609