Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to convert list to comma separated string using java 8

  • Howto convert list to comma separated string using java 8 stream.
  • By using collect() method of stream  and Collectors.join(",") method we can convert list to comma separated string in java 8.
  • Java Example program to convert list of strings to comma separated string.



#1: Java Example program to convert list to Comma Separated String using java 8 stream.

  1. package com.instanceofjava;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.stream.Collectors;

  5. /**
  6.  * @author www.Instanceofjava.com
  7.  * @category interview programming questions
  8.  * 
  9.  * Description: convert ArrayList of strings tp comma separated string using java 8
  10.  *
  11.  */
  12. public class ArrayToString {

  13. public static void main(String[] args) {
  14. ArrayList colours =new ArrayList();
  15. colours.add("Red");
  16. colours.add("Green");
  17. colours.add("Orange");
  18.    String result = colours.stream().collect(Collectors.joining(","));
  19.    System.out.println(result);
  20.    
  21. }
  22. }


Output:
  1. Red,Green,Orange





This post first appeared on Java Tutorial - InstanceOfJava, please read the originial post: here

Share the post

How to convert list to comma separated string using java 8

×

Subscribe to Java Tutorial - Instanceofjava

Get updates delivered right to your inbox!

Thank you for your subscription

×