Why is StringTokenizer deprecated?
Solution 1
From the javadoc for StringTokenizer:
StringTokenizer
is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
If you look at String.split()
and compare it to StringTokenizer
, the relevant difference is that String.split()
uses a regular expression, whereas StringTokenizer
just uses verbatim split characters. So if I wanted to tokenize a string with more complex logic than single characters (e.g. split on \r\n
), I can't use StringTokenizer
but I can use String.split()
.
Solution 2
- Java 10 String Tokenizer -- not deprecated
- Java 9 String Tokenizer -- not deprecated
- Java 8 String Tokenizer -- not deprecated
- Java 7 String Tokenizer -- not deprecated
- Java 6 String Tokenizer -- not deprecated
- Java 5 String Tokenizer -- not deprecated
If it is not marked as deprecated, it is not going away.
Solution 3
StringTokenizer is not deprecated in fact StringTokenizer is 4X faster than String.split() and in competitive programming it is used by many developers.
Source :- Faster Input for Java
Solution 4
There is an issue with StringTokenize ...
Split have to use regex, StringTokenizer is using String or CharSequence,
but
"a.b..".split(".")
will return {"a","b",""}
and StringTokenizer of "a.b.." ... will return only {"a","b"}
And this is very tricky!!! Be Carefull!!!
Better and safer alternatives to StringTokenizer are:
Much better StrongTokenizer
is in org.apache.common.lang3
... it have much more flexibility or
com.google.common.base.Splitter
donnyton
Updated on July 09, 2022Comments
-
donnyton almost 2 years
The Java documentation doesn't seem to mention anything about deprecation for
StringTokenizer
, yet I keep hearing about how it was deprecated long ago. Was it deprecated because it had bugs/errors, or isString.split()
simply better to use overall?I have some code that uses
StringTokenizer
and I am wondering if I should seriously be concerned about refactoring it to useString.split()
, or whether the deprecation is purely a matter of convenience and my code is safe. -
Ali over 12 yearsYou're right, it's not, it's just recommended that you dont use it. The difference being that there is no guaratee that deprecated classes will continue to be provided in future releases.
-
user207421 over 12 yearsThat's not correct either. There is an absolute guarantee of backwards binary compatibility.
-
Miuler over 11 years"StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead."
-
StackOverflowed over 11 yearsLegacy is different than deprecated.
-
Audrius Meškauskas over 11 yearsPersonally for me, a code using StringTokenizer looks simpler and cleaner.
-
Scott Chu over 7 yearsExactly. And this issues took our programmers tremendous efforts to find a 'butterfly effect' bug. Why Java team invents such terrible stuff in the beginning?... Sigh!
-
user1568901 over 7 yearsYeah, I'm with @h22, often StringTokenizer code is more elegant/readable than using split. I save split for when I really need the newer behavior.
-
RBz almost 7 yearsIs this still valid?
-
David 天宇 Wong over 6 yearsaccording to other answers yes, it's just that split is more flexible since you can use regexes