HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / String / Java Split CSV String – Convert String to List Example

Java Split CSV String – Convert String to List Example

Learn to split string by comma or space and store in array or arraylist. Use given Java program to convert string to List in Java.

1. Convert String to List Using Regular Expression

We can use regular expression "\\s*,\\s*" to match comma in CSV string and then do Java string split with String.split() method to convert string to list.

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

public class CSVSplitExample 
{
	public static void main(String[] args) 
	{
		String alpha = "how, to, do, in, java";

		//Remove whitespace and split by comma
		List<String> result = Arrays.asList(alpha.split("\\s*,\\s*"));

		System.out.println(result);
	}

}

Program Output.

[how, to, do, in, java]

This resulting ArrayList is a fixed-size unmodifiable read-only list backed by the array. It means you cannot add or remove elements from this list. If you want a modifiable list then use next approach.

Notice split() function returns string array. So you can modify above example to split string to array as well.

2. Convert CSV to Mutable ArrayList

To get the mutable arraylist, copy all elements from read-only list received from above example into a new ArrayList object.

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

public class CSVSplitExample2 
{

	public static void main(String[] args) 
	{
		String alpha = "how, to, do, in, java";

		//Typecast to ArrayList
		List<String> result = new ArrayList<String>( Arrays.asList(alpha.split("\\s*,\\s*")) );

		System.out.println(result);

		result.add("com");

		System.out.println(result);
	}
}
[how, to, do, in, java, com]

3. Convert List to CSV String – Java 8

If we want to convert list to CSV, then we can use String.join() method provided by Java 8.

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

public class JavaListToStringExample 
{
	public static void main(String[] args) {
		 
		List<String> list = Arrays.asList("how", "to", "do", "in", "java");

		String result = String.join("-", list);		//delimited by comma
		System.out.println(result);
		
		String result2 = String.join(" ", list);	//delimited by space
		System.out.println(result2);
    }
}
Output: 

Program Output.

how-to-do-in-java
how to do in java

Above examples will help you to convert String to List and also convert List to String in Java.

Happy Learning !!

Read More:

Read/Write CSV file with OpenCSV
Read/Write CSV file with SuperCSV
3 examples to parse CSV files
Join String in Java 8

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Keith Rust

    February 19, 2020

    How do you properly split the string if a comma is part of the data in one of the elements and it’s quoted?

    “\”ho,w\”,\” to\”,\” do\”,\” in\”,\” ja,v\”\”a\””

    This needs to return the following splits:

    “ho,w”
    ” to”
    ” do”
    ” in”
    ” ja,v””a”

    • Keith Rust

      February 19, 2020

      Of course, my quote escapes are removed from the post. 😀

  2. Martin

    October 21, 2018

    String result = String.join(“-“, list); //delimited by comma

    the comment should be: // delimited by “-“

  3. Colin Richardson

    June 2, 2017

    Output of last example is wrong

    • Lokesh Gupta

      June 2, 2017

      True. It’s corrected now. Thanks for pointing out.

  4. Facebamm

    June 2, 2017

    You have a misstake in the last exempel by the out.

    Join by “,”, thats mean you have between all listitems an “,”

    The Output must showing like this “how,to,do,in,java”

    • Lokesh Gupta

      June 2, 2017

      True. It’s corrected now. Thanks for pointing out.

Comments are closed on this article!

Search Tutorials

String methods

  • String concat()
  • String hashCode()
  • String contains()
  • String compareTo()
  • String compareToIgnoreCase()
  • String equals()
  • String equalsIgnoreCase()
  • String charAt()
  • String indexOf()
  • String lastIndexOf()
  • String intern()
  • String split()
  • String replace()
  • String replaceFirst()
  • String replaceAll()
  • String substring()
  • String startsWith()
  • String endsWith()
  • String toUpperCase()
  • String toLowerCase()

String examples

  • Convert String to int
  • Convert int to String
  • Convert String to long
  • Convert long to String
  • Convert CSV String to List
  • Java StackTrace to String
  • Convert float to String
  • String – Alignment
  • String – Immutable
  • String – StringJoiner
  • Java – Split string
  • String – Escape HTML
  • String – Unescape HTML
  • String – Convert to title case
  • String – Find duplicate words
  • String – Left pad a string
  • String – Right pad a string
  • String – Reverse recursively
  • String – Leading whitespaces
  • String – Trailing whitespaces
  • String – Remove whitespaces
  • String – Reverse words
  • String – Find duplicate characters
  • String – Check empty string
  • String – Get first 4 characters
  • String – Get last 4 characters
  • String – (123) 456-6789 pattern
  • String – Interview Questions

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)