Skip to content

Java StringTokenizer – Get ArrayList of Tokens

Java StringTokenizer – get ArrayList of tokens example shows how to get the ArrayList or list of tokens from the StringTokenizer object.

How to get token ArrayList from the StringTokenizer object?

The StringTokenizer class is used to create tokens from the string content. These tokens are generated from the string using the delimiter we provide.

Once the string tokens are generated, we can access these tokens using the hasMoreTokens and nextToken methods. The StringTokenizer class also provides Enumeration methods hasMoreElements and nextElements methods to access the tokens.

These methods allow us to access the tokens in a sequential way. If we want to access StringTokenizer tokens using the index, we cannot do that because this class does not have any such methods. However, we can convert the tokens to Java ArrayList or any other list object that will allow us to get the elements using an index.

Please see the below-given code example in which I have created a method that takes the string content and delimiter to use as parameters and returns an ArrayList of tokens.

Output

Important Note:

If you need an ArrayList of tokens, you should use the Java String split method instead of the StringTokenizer class. The StringTokenizer class is only retained for compatibility purposes.

If you want to learn more about the tokenizer, please visit the Java StringTokenizer tutorial.

Please let me know your views in the comments section below.

About the author

Leave a Reply

Your email address will not be published.