{

How to convert List into comma separated string


In this blog post, How to convert List in comma-delimited string. It covers the following examples for

  • Convert the List of custom objects into a comma-separated string
  • Convert List of Strings/Long/Integers into a comma-separated string.
Convert list into comma separated string

How to convert a List of custom objects into a comma-separated string

There are multiple ways to convert a list of objects into comma-separated strings.

here object can be the custom object of the java class.

For example, the User object declares with the following member’s attributes.

package com.company;

public class User {
    private Integer id;
    private String name;

    public User(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Let’s insert the data for this example.

        List<User> users = new ArrayList<User>();
        User user1 = new User (1,"Eric");

        User user2 = new User (2,"John");
        User user3 = new User (3,"Ram");

        users.add(user1);
        users.add(user2);
        users.add(user3);

Using for loop

for each loop is used to iterate the list.

Here is the sequence of steps

  • Create a String builder object
  • use forEach loop to iterate each object
  • append object name and comma delimiter
  • Convert StringBuilder to String using toString() method
  • As we have a comma added to the last string.
  • Remove the last comma using the substring method

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Main {

    public static void main(String[] args) {
        List<User> users = new ArrayList<User>();
        User user1 = new User (1,"Eric");

        User user2 = new User (2,"John");
        User user3 = new User (3,"Ram");

        users.add(user1);
        users.add(user2);
        users.add(user3);
        StringBuilder sb = new StringBuilder();

        for(User user:users){
            sb.append(user.getName()).append(",");
        }
        String commaDelimeterString=sb.toString();
        if( commaDelimeterString.length() > 0 )
            commaDelimeterString = commaDelimeterString.substring(0, commaDelimeterString.length() - 1);


        System.out.println(commaDelimeterString);

    }
}

Using java8 streams

java8 introduced streams to manipulate the list or array of objects.

You can check below tutorials java8 streams

  • java8 stream examples

  • [Stream map] (/java8-stream-map-examples)

  • java8 lambda expression examples

  • lambda expression scopes

  • list object stream is called using stream() method

  • Iterate each object using map method and lambda expression

  • get object email in a lambda expression

  • java.util.stream.Collectors method joining uses comma delimiter and returns the string comma delimiter using reducer called collect.

here is an complete example

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Main {

    public static void main(String[] args) {
        List<User> users = new ArrayList<User>();
        User user1 = new User (1,"Eric");
        User user2 = new User (2,"John");
        User user3 = new User (3,"Ram");

        users.add(user1);
        users.add(user2);
        users.add(user3);
        String nameSeparatedString = users.stream().map(user->user.getName())
                .collect(Collectors.joining(","));
        System.out.println(nameSeparatedString);

    }
}

Output:

Eric,John,Ram

Convert List of strings into a comma-separated string

In these tutorials, We will see how to convert a primitive list of String, Integer,Long into delimited separated strings.

apache commons StringUtils join method

This is easy and simple to convert a string list to comma-separated strings.

If you are using the apache commons library in your application, You can use StringUtils join() method

The same method is available in apache commons-lang module also i.e org.apache.commons.lang3.StringUtils

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;

public class Main {

    public static void main(String[] args) {
        List<String> strList = new ArrayList<>();
        strList.add("one");
        strList.add("two");
        strList.add("three");
        strList.add("four");
        String delimetedCommaString=StringUtils.join(strList, ',');
        System.out.println(delimetedCommaString);
    }
}

Output

one,two,three,four

Spring framework to convert List of Integer into a comma-separated string

Spring framework provides StringUtils class in org.springframework.util package.

This class has arrayToDelimitedString method

Here is the syntax of the method

public static String arrayToDelimitedString(@Nullable
                                            Object[] arr,
                                            String delim)

Here is an example converting list of integers into comma separated string Code

import java.util.ArrayList;
import java.util.List;
import org.springframework.util.StringUtils;

public class Main {

    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(12);
        list.add(14);
        list.add(50);
        String delimetedCommaString=arrayToDelimitedString(list, ',');
        System.out.println(delimetedCommaString);
    }
}
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.