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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
package com.javacodeexamples.stringtokenizerexamples; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; public class StringTokenizerToList { public static void main(String[] args) { List<String> list = getTokensArrayList("StringTokenizer to ArrayList", " "); for(String strToken : list) { System.out.println( strToken ); } } private static List<String> getTokensArrayList(String content, String delimiters){ List<String> aListTokens = new ArrayList<String>(); try { if(content != null && delimiters != null) { //create tokenizer object StringTokenizer sTokenizer = new StringTokenizer(content, delimiters); //iterate over tokens while( sTokenizer.hasMoreTokens() ) { //add to an ArrayList aListTokens.add( sTokenizer.nextToken() ); } } }catch(Exception e) { e.printStackTrace(); } return aListTokens; } } |
Output
1 2 3 |
StringTokenizer to ArrayList |
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.