Convert list of integers to comma separated string java

How to convert List into comma separated string

In this blog post, How to convert List in comma delimited string. This covers an following examples for

  • Convert List of custom objects into comma separated string
  • Convert List of Strings/Long/Integers into comma separated string.

How to convert List of custom objects into comma separated string

There are multiple ways to convert list of objects into comma separated string.

here object can be custom object of java class.

For example, User object is declared with following members 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; } }

Lets insert the data for this example

List users = new ArrayList[]; 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 are 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 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 users = new ArrayList[]; 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 lambda expression
  • java.util.stream.Collectors method joining uses comma delimiter and return 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 users = new ArrayList[]; 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 comma separated string

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

apache commons StringUtils join method

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

If you are using 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 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 comma separated string

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

This class has arrayToDelimitedString method

Here is the syntax of 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 list = new ArrayList[]; list.add[1]; list.add[12]; list.add[14]; list.add[50]; String delimetedCommaString=arrayToDelimitedString[list, ',']; System.out.println[delimetedCommaString]; } }

Video liên quan

Chủ Đề