添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
大力的冰棍  ·  BufferedReader 和 ...·  13 小时前    · 
深沉的野马  ·  JavaIO流 ...·  13 小时前    · 
捣蛋的牙膏  ·  java ...·  13 小时前    · 
霸气的跑步机  ·  Strings with ...·  7 小时前    · 
谦和的沙滩裤  ·  主题配置 | ...·  3 小时前    · 
怕考试的打火机  ·  iOS15.7.6下载_iPod ...·  3 月前    · 
活泼的作业本  ·  Error During setData ...·  5 月前    · 
才高八斗的橙子  ·  查堵点 破难题 ...·  9 月前    · 

There are many ways to convert a comma-separated string into a list in Java. In this article, we'll look at three different methods to convert a string with a separator to a list.

Convert a comma-separated string to a list using core Java

The String class in Java provides split() method to split a string into an array of strings. You can use this method to turn the comma-separated list into an array:

String fruits = "🍇,🍓,🍑,🥭,🍍,🥑";
String [] fruitsArray = fruits.split(",");

Next, use the Arrays.asList() method to convert the array into a list:

List<String> fruitsList = Arrays.asList(fruitsArray);

That's it. Here is the complete example code:

String fruits = "🍇,🍓,🍑,🥭,🍍,🥑";
List<String> fruitsList = Arrays.asList(fruits.split(","));
System.out.println(fruitsList);
// [🍇, 🍓, 🍑, 🥭, 🍍, 🥑]

If the comma-separated string contains white spaces, you can pass a regular expression to split() to remove them:

String fruits = "🍇, 🍓, 🍑, 🥭, 🍍, 🥑";
List<String> fruitsList = Arrays.asList(fruits.split("\\s*,\\s*"));

Convert a comma-separated string to a list using Java streams

Java Stream API can also be used to convert to comma-separated string into a list, as shown below:

String fruits = "🍇, 🍓, 🍑, 🥭, 🍍, 🥑";
List<String> fruitsList = Stream.of(fruits.split("\\s*,\\s*"))
        .collect(Collectors.toList());
System.out.println(fruitsList);
// [🍇, 🍓, 🍑, 🥭, 🍍, 🥑]

In the above example, we first used the split() method to convert our fruits string into an array of strings. Then, we used the Stream class to convert the array into a list of strings.

An additional benefit of using Java Stream API is that you can perform other operations on the elements of the array before converting them into a list.

Look at the following example that converts a string of numbers into a list of integers using a stream:

String numbers = "23, 45, 2, 7, 99, 6";
List<Integer> list = Stream.of(numbers.split(","))
        .map(String::trim)
        .map(Integer::parseInt)
        .collect(Collectors.toList());
System.out.println(list);
// [23, 45, 2, 7, 99, 6]

The first part of the example is the same, convert a comma-separated string of numbers into an array.

Then, it trims the leading and trailing spaces from each string on the stream using the map(String::trim) method.

Next, the map(Integer::parseInt) method is called on our stream to convert every string to an Integer.

Finally, it calls the collect(Collectors.toList()) method on the stream to transform it into an integer list.

Convert a comma-separated string to a list using Apache Commons Lang

Apache Commons Lang is an open-source library that provides many utility classes to manipulate core Java classes.

One such utility class is the StringUtils that offers utility methods for string operations.

To add Commons Lang to your Maven project, add the following dependency to the pom.xml file:

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-lang3</artifactId>
  <version>3.12.0</version>
</dependency>

For Gradle, add the below dependency to your build.gradle file:

implementation 'org.apache.commons:commons-lang3:12.0'

Now you can use the StringUtils.splitPreserveAllTokens() method to convert the string into an array of strings:

String[] fruitsArray =  StringUtils.splitPreserveAllTokens(fruits, ",");

Next, use the Arrays.asList() method to transform the array into a list:

List<String> fruitsList = Arrays.asList(fruitsArray);

Both split() and splitPreserveAllTokens() methods split the string into an array of strings using a delimiter. However, the splitPreserveAllTokens() method preserves all tokens, including the empty strings created by adjoining separators, while the split() method ignores empty strings.

Read Next: Convert a list to a comma-separated string in Java

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development. The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.