Converting StringTokenizer tokens into ArrayList in Java

In this article, we will discuss how to convert StringTokenizer tokens into ArrayList

Few things to note:

 

Let us move forward to discuss an example on how to convert StringTokenizer tokens into ArrayList

Example 1: Converting StringTokenizer tokens into ArrayList

Syntax:

StringTokenizer st = new StringTokenizer(str, “ ”);

Steps:

  • First define a sample string
  • Create StringTokenizer object and pass sample string as 1st constructor-argument with space (” “) as 2nd constructor-argument
  • Create ArrayList object
  • Now, iterate through tokens and simultaneously add tokens iterated into List

ConvertTokensIntoArrayList.java

package in.bench.resources.java.stringtokenizer.example;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class ConvertTokensIntoArrayList {

	public static void main(String[] args) {

		// sample string
		String str = "Water Wind Earth Sky Fire";

		// create StringTokenizer object
		StringTokenizer st = new StringTokenizer(str, " ");

		// create ArrayList object
		List<String> elements = new ArrayList<String>();

		// iterate through StringTokenizer tokens
		while(st.hasMoreTokens()) {

			// add tokens to AL
			elements.add(st.nextToken());
		}

		// original String
		System.out.println("Original String : \n" + str);

		// iterate through AL - using forEach loop
		System.out.println("\nPrinting elements in ArrayList ...");
		for(String element : elements) {
			System.out.println(element);
		}
	}
}

Output:

Original String : 
Water Wind Earth Sky Fire

Printing elements in ArrayList ...
Water
Wind
Earth
Sky
Fire

 

Alternative approach is to split() method of String class as StringTokenizer is deprecated

 

Example 2: Splitting String into Arrays and then converting into ArrayList

Syntax:

String[] strArray = testStr.split(" ");

List<String> ls = new ArrayList<String>(Arrays.asList(strArray));

Steps:

  • First define a sample string
  • Then split sample string using space (“ ”) as delimiter
  • Create new ArrayList object and add string array values using constructor-argument as shown in the syntax

ConvertStringArrayIntoArrayList.java

package in.bench.resources.java.stringtokenizer.example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ConvertStringArrayIntoArrayList {

	public static void main(String[] args) {

		// sample string
		String str = "Fire Earth Sky Water Wind";

		// split string using space as delimiter
		String[] strArray = str.split(" ");

		// create ArrayList object
		List<String> elements = new ArrayList<String>(Arrays.asList(strArray));

		// original String
		System.out.println("Original String : \n" + str);

		// iterate through AL - using forEach loop
		System.out.println("\nPrinting elements in ArrayList ...");
		for(String element : elements) {
			System.out.println(element);
		}
	}
}

Output:

Original String : 
Fire Earth Sky Water Wind

Printing elements in ArrayList ...
Fire
Earth
Sky
Water
Wind

 

Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

 

References:

https://docs.oracle.com/javase/6/docs/api/java/util/StringTokenizer.html
https://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html
https://docs.oracle.com/javase/8/docs/api/java/util/StringTokenizer.html
https://docs.oracle.com/javase/6/docs/api/java/lang/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html

 

Happy Coding !!
Happy Learning !!