Convert a delimited String into a List in Java
This post will discuss how to convert a delimited String into a List in Java.
There is no built-in method to convert a delimited string into a List in Java. However, we can solve this problem by dividing it into two parts: splitting the string, and creating a list from the resulting iterable. This can be done using any of the following methods:
1. Using String.split() method
The simplest and most common way to split the string by a delimiter using the String.split() method. Then, convert the resultant string array into a list using the Arrays.asList() method. For example, the following code splits the string using the regex \s*-\s* as a delimiter. It matches with zero or more whitespace characters, followed by a hyphen, followed by zero or more whitespace. This will basically trim any whitespace around the hyphens and split the string into substrings.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { String hyphenSeparated = "a-b-c-d"; List<String> tokens = Arrays.asList(hyphenSeparated.split("\\s*-\\s*")); System.out.println(tokens); // [a, b, c, d] } } |
The Arrays.asList() method returns a fixed-size list backed by the specified array. If we need a mutable instance of the list, we can pass the fixed-size list to the ArrayList constructor.
2. Using Stream API
To filter or map the substrings, consider using the Stream API with the split() method. The Stream.of() method creates a stream from the array of substrings returned by the split() method. The collect() method collects the elements of the stream into a list using the Collectors.toList() collector. Here is an example of using the Stream API:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; class Main { public static void main(String[] args) { String hyphenSeparated = "a -b-c-d"; List<String> tokens = Stream.of(hyphenSeparated.split("-")) .map(String::trim) .collect(Collectors.toList()); System.out.println(tokens); // [a, b, c, d] } } |
3. Using Guava Library
We can also use the Splitter class from Guava library to split the string. The Splitter class allows us to create a splitter object that can split a string by a delimiter and return a list of substrings. To illustrate, consider the following example. The Splitter.on() method takes the delimiter as an argument and creates a splitter object. The trimResults() method tells the splitter to remove any whitespace around the delimiter. The Lists.newArrayList() method takes the string to be split and returns an ArrayList of substrings.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import com.google.common.base.Splitter; import com.google.common.collect.Lists; import java.util.List; class Main { public static void main(String[] args) { String hyphenSeparated = "a-b-c-d"; Iterable<String> iterable = Splitter.on("-").trimResults().split(hyphenSeparated); List<String> tokens = Lists.newArrayList(iterable); System.out.println(tokens); // [a, b, c, d] } } |
4. Using StringTokenizer class
The StringTokenizer class is another way to split a string by a delimiter and create an iterable object that can be used to create a list. To illustrate, consider the following example. The StringTokenizer constructor takes the string to be split and the delimiter as arguments. The hasMoreTokens() method returns true if there are more substrings to be extracted, and the nextToken() method returns the next substring. The trim() method removes any leading or trailing whitespace from the substring.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; class Main { public static void main(String[] args) { String hyphenSeparated = "a-b-c-d"; StringTokenizer tokenizer = new StringTokenizer(hyphenSeparated, "-"); List<String> tokens = new ArrayList<>(); while (tokenizer.hasMoreTokens()) { tokens.add(tokenizer.nextToken().trim()); } System.out.println(tokens); // [a, b, c, d] } } |
5. Using Scanner class
A final option is to use the Scanner class to split a string by a delimiter and create a list of substrings. To illustrate, consider the following example. The Scanner constructor takes the string to be split as an argument. The useDelimiter() method sets the delimiter pattern to be used by the scanner. The hasNext() method returns true if there is another substring to be read, and the next() method returns the next substring. The close() method closes the scanner.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.ArrayList; import java.util.List; import java.util.Scanner; class Main { public static void main(String[] args) { String hyphenSeparated = "a -b-c-d"; Scanner scanner = new Scanner(hyphenSeparated).useDelimiter("-"); List<String> tokens = new ArrayList<>(); while (scanner.hasNext()) { tokens.add(scanner.next().trim()); } scanner.close(); System.out.println(tokens); // [a, b, c, d] } } |
That’s all about converting a delimited String into a List in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)