Convert List String to List int Java 8

In this post, we will see how to convert List to String in java.
Following are the ways to convert list into String:

Table of Contents

  • Using StringBuilder
  • Using + operator
  • Using Strings join method in java 8
  • Using Apache common lang3
  • Using Guava library
  • Print elements of list as String

Using StringBuilder

We can use StringBuilder class to convert List to String. StringBuilder is the best approach if you have other than String Array, List.We can add elements in the object of StringBuilder using the append[] method while looping and then convert it into string using toString[] method of String class at the end.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package org.arpit.java2blog;
import java.util.List;
import java.util.ArrayList;
import java.lang.String;
public class ConvertListToStringMain
{
public static void main[String arg[]]
{
//creationand initialization of list
List list=new ArrayList[];
list.add["Mary"];
list.add["Martin"];
list.add["John"];
list.add["Newton"];
//print content of list
System.out.print["Elements of list are:"];
System.out.println[list];
//conversion of ArrayList to String
StringBuilder strbul=new StringBuilder[];
for[String str : list]
{
strbul.append[str];
//for adding comma between elements
strbul.append[","];
}
//just for removing last comma
//strbul.setLength[strbul.length[]-1];
String str=strbul.toString[];
System.out.println["Converted String is " + str];
}
}

OUTPUT:

Elements of list are:[Mary, Martin, John, Newton] Converted String is Mary,Martin,John,Newton,

Using + operator

This is not an efficient way so, normally avoid it.We use concate[+] operator to add one by one element from array inside loop and assign it into String.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package org.arpit.java2blog;
//import all these packages
import java.util.List;
import java.util.ArrayList;
import java.lang.String;
import java.util.Arrays;
public class ListToStringPlusMain
{
public static void main[String arg[]]
{
//creationand initialization of list
List list=new ArrayList[];
list.add["Mary"];
list.add["Martin"];
list.add["John"];
list.add["Newton"];
//print content of list
System.out.print["Elements of list are:"];
System.out.println[list];
//conversion of ArrayList into String using +
String str="";
for[String s : list]
{
str=str + s + ",";
}
System.out.println["Converted String is " + str];
}
}

OUTPUT:

Elements of list are:[Mary, Martin, John, Newton] Converted String is Mary,Martin,John,Newton,

Using Strings join method in java 8

Strings join method is introduced in Java 8 . This method is very handy to create delimited separated String from list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package org.arpit.java2blog;
//import all these packages
import java.util.List;
import java.util.ArrayList;
import java.lang.String;
public class ListToStringJoinMain
{
public static void main[String arg[]]
{
//creationand initialization of list
List list=new ArrayList[];
list.add["Mary"];
list.add["Martin"];
list.add["John"];
list.add["Newton"];
//print content of list
System.out.print["Elements of list are:"];
System.out.println[list];
//conversion of ArrayList into String using +
String str=String.join[",", list];
System.out.println["Converted String is " + str];
}
}

This approach will work only for List of String. In case, if you have list of Double, then you can use java 8s Collectors to join the String.
Here is an example:

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package org.arpit.java2blog;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class ConvertListToStringMain
{
public static void main[String arg[]]
{
//creationand initialization of list
List list=new ArrayList[];
list.add[34.2];
list.add[29.7];
list.add[40.2];
list.add[50.4];
//print content of list
System.out.print["Elements of list are:"];
System.out.println[list];
//conversion of ArrayList into String using Java 8's Collector
String str = list.stream[].map[Object::toString]
.collect[Collectors.joining[","]];
System.out.println["Converted String is: " + str];
}
}

Output:

Elements of list are:[34.2, 29.7, 40.2, 50.4] Converted String is: 34.2,29.7,40.2,50.4

As you can see, we have used Collectors.joining[","] to join elements of String by ,

Using Apache common lang3

You can also use Apache commons StringUtils to convert list to String.
Here is the dependency which you need to add for Apache common lang3 in pom.xml.

XHTML
1
2
3
4
5
6
7
org.apache.commons
commons-lang3
3.9

Here is the example:

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package org.arpit.java2blog;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
public class ConvertStringToListApache
{
public static void main[String arg[]]
{
List intList= Arrays.asList[new Integer[] {1,2,3,4}];
//print content of list
System.out.print["Elements of list are:"];
System.out.println[intList];
String join = StringUtils.join[intList,"*"];
System.out.println["Converted String is: " + join];
}
}
Elements of list are:[1, 2, 3, 4] Converted String is: 1*2*3*4

Using Guava library

You can also use Guavas Joiner class to convert list to String.
Here is the dependency which you need to add for guava in pom.xml.

XHTML
1
2
3
4
5
6
7
com.google.guava
guava
29.0-jre

Here is the example:

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package org.arpit.java2blog;
import java.util.Arrays;
import java.util.List;
import com.google.common.base.Joiner;
public class ConvertListToStringGuava
{
public static void main[String arg[]]
{
List intList= Arrays.asList[new Integer[] {1,2,3,4}];
//print content of list
System.out.print["Elements of list are:"];
System.out.println[intList];
String join = Joiner.on["*"].join[intList];
System.out.println["Converted String is: " + join];
}
}
Elements of list are:[1, 2, 3, 4] Converted String is: 1*2*3*4

As you can see, we have used com.google.common.base.Joiner class to join elements of String by *

Print elements of list as String

Sometimes, we just need to print elements of the list. We can simply use toString[] method
Lets understand with the help of example.

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package org.arpit.java2blog;
import java.util.Arrays;
import java.util.List;
public class PrintListOfElementsMain
{
public static void main[String arg[]]
{
List intList= Arrays.asList[new Integer[] {1,2,3,4}];
//print content of list
System.out.print["Elements of list are:"];
System.out.println[intList];
}
}

Output:

Elements of list are:[1, 2, 3, 4]

Did you notice we did not even call toString[] method on intList object?
JVM internally calls toString[] method of type of element within this list. In this case, we have list of Integers, so it will call Integers toString[] method.

In case, you have custom object, then you should override toString[] method, else it will give you unexpected results.
Lets see with the help of example
Create a simple class named Color.java

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package org.arpit.java2blog;
public class Color {
String name;
String htmlCode;
public Color[String name, String htmlCode] {
super[];
this.name = name;
this.htmlCode = htmlCode;
}
public String getName[] {
return name;
}
public void setName[String name] {
this.name = name;
}
public String getHtmlCode[] {
return htmlCode;
}
public void setHtmlCode[String htmlCode] {
this.htmlCode = htmlCode;
}
}

Create main class named PrintListOfColorsMain.java

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package org.arpit.java2blog;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class PrintListOfColorsMain {
public static void main[String[] args] {
// Sort list of colors by name
List listOfColors = getListOfColors[];
System.out.println[listOfColors];
}
public static List getListOfColors[] {
List listOfColors = new ArrayList[];
Color red = new Color["Red", "#FF0000"];
Color blue = new Color["Blue", "0000FF"];
Color white = new Color["White", "#FFFFFF"];
Color green = new Color["Green", "#008000"];
listOfColors.add[red];
listOfColors.add[blue];
listOfColors.add[white];
listOfColors.add[green];
return listOfColors;
}
}

Output:

[[emailprotected], [emailprotected], [emailprotected], [emailprotected]]

If you notice, we dont have toString[] method in Color class, thats why it is calling Objects toString[] method.

We need to override toString[] method in Color class and we will get informative output.

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package org.arpit.java2blog;
public class Color {
String name;
String htmlCode;
public Color[String name, String htmlCode] {
super[];
this.name = name;
this.htmlCode = htmlCode;
}
public String getName[] {
return name;
}
public void setName[String name] {
this.name = name;
}
public String getHtmlCode[] {
return htmlCode;
}
public void setHtmlCode[String htmlCode] {
this.htmlCode = htmlCode;
}
@Override
public String toString[] {
return "Color [name:"+name+" HtmlCode:"+htmlCode+"]";
}
}

When you run PrintListOfColorsMain again, you will get below output:

[Color [name:Red HtmlCode:#FF0000], Color [name:Blue HtmlCode:0000FF], Color [name:White HtmlCode:#FFFFFF], Color [name:Green HtmlCode:#008000]]

As you can see, we have much meaningful output now.

Thats all about converting List to String in java.

Video liên quan

Chủ Đề