list to string java-looking to convert?
Let's have a glance at how to convert a Java Collections List of elements to a
String in Java.
![]() |
| list to string java |
Basically List in String Java is an ordered collection or a default sequence.
List accepts duplicate elements unlike Map doesn't accept duplicate values and also holds the thing in key-value pairs.
We can print the contents of a listing element during a readable form while debugging the code, which is useful .
List interface and String class were a part of Java object-oriented programming API.
We can add any sort of Java object to a list. If the List doesn't type, then using Java Generics we will apply objects of various types within the same List.
Typically we will enclose the generic type in square
brackets.
Using toString()
From below Java program, lets see how to convert Java list to string array using toString method.
We are passing integer argument (number) to create java string array using Arrays asList() method.
In other words, we are passing list of integers as array to list.
public static void main(String[] args) {
try {
List<Integer> list = Arrays.asList(0, 1, 2, 3);
System.out.println(list);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
[0, 1, 2, 3]
As per the output code above, we can see list of array values printed as array of strings or string array in java.
This way of implementation uses the inbuilt toString() method within the List.
Here Integer Java generics type has a internal implementation of toString() method.
In above example we used Arrays.asList to create a array in java via optimal manner.
But we can also use standard ArrayList in java and can add the values using list.add() method or addAll method.
Also, we can convert from traditional ArrayList to String Array using ArrayList Class.
Interview Question 1 -> Primitive Types in Java:
boolean , byte , char , short , int , long , float and double are the primitive types in Java API.
From below Java program, lets see how to convert Java list to string array using toString method.
We are passing integer argument (number) to create java string array using Arrays asList() method.
In other words, we are passing list of integers as array to list.
public static void main(String[] args) {
try {
List<Integer> list = Arrays.asList(0, 1, 2, 3);
System.out.println(list);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
[0, 1, 2, 3]
As per the output code above, we can see list of array values printed as array of strings or string array in java.
This way of implementation uses the inbuilt toString() method within the List.
Here Integer Java generics type has a internal implementation of toString() method.
In above example we used Arrays.asList to create a array in java via optimal manner.
But we can also use standard ArrayList in java and can add the values using list.add() method or addAll method.
Also, we can convert from traditional ArrayList to String Array using ArrayList Class.
Interview Question 1 -> Primitive Types in Java:
boolean , byte , char , short , int , long , float and double are the primitive types in Java API.
List of Object to String
-
Lets see how Object toString() method works as per below java program class.
public class HelloObject {
String name;
int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "HelloObject [name=" + name + ", age=" + age + "]";
}
}
Here the custom toString() function which returns in a custom string object format.
public static void main(String[] args) {
try {
List<HelloObject> list = new ArrayList<HelloObject>();
HelloObject hb = new HelloObject();
hb.setName("James");
hb.setAge(25);
list.add(hb);
System.out.println(List);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
[HelloObject [name=James, age=25]]
Here custom toString() in the HelloObject will convert the Object into String representation format.
As per output, we can see the list of string.
Interview Question 2 -> ArrayList vs LinkedList:
Array List in java api applies a dynamic array to store the elements.
Where as LinkedList uses a double linked list to store the elements.
Also ArrayList String value can convert to byte array using Java programming language.
Again both ArrayList and LinkedList accepts duplicate values.
But we can remove duplicates using plain Java code, Lambdas, Guava.
Lets see how Object toString() method works as per below java program class.
public class HelloObject {
String name;
int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "HelloObject [name=" + name + ", age=" + age + "]";
}
}
Here the custom toString() function which returns in a custom string object format.
public static void main(String[] args) {
try {
List<HelloObject> list = new ArrayList<HelloObject>();
HelloObject hb = new HelloObject();
hb.setName("James");
hb.setAge(25);
list.add(hb);
System.out.println(List);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
[HelloObject [name=James, age=25]]
Here custom toString() in the HelloObject will convert the Object into String representation format.
As per output, we can see the list of string.
Interview Question 2 -> ArrayList vs LinkedList:
Array List in java api applies a dynamic array to store the elements.
Where as LinkedList uses a double linked list to store the elements.
Also ArrayList String value can convert to byte array using Java programming language.
Again both ArrayList and LinkedList accepts duplicate values.
But we can remove duplicates using plain Java code, Lambdas, Guava.
Join Method (StringUtils)
-
We can use the join method of Apache Commons Lang StringUtils class to achieve the java list to string conversion.
public static void main(String[] args) {
try {
List<Integer>list = Arrays.asList(0, 1, 2, 3);
System.out.println(StringUtils.join(list, " "));
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
0 1 2 3
StringUtils.join method have inbuilt toString() method.
As we can see from above output, it prints the List elements as String data type with space delimiter.
we can also use java regular expressions to define a search pattern for strings.
Regular expression is best applicable for pattern matching of expressions or functions.
Here is the maven dependency for Apache commons lang StringUtils class of java api.
you can use this in your project pom.xml file as dependency.
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
The latest version of the dependency will be available here.
We can use the join method of Apache Commons Lang StringUtils class to achieve the java list to string conversion.
public static void main(String[] args) {
try {
List<Integer>list = Arrays.asList(0, 1, 2, 3);
System.out.println(StringUtils.join(list, " "));
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
0 1 2 3
StringUtils.join method have inbuilt toString() method.
As we can see from above output, it prints the List elements as String data type with space delimiter.
we can also use java regular expressions to define a search pattern for strings.
Regular expression is best applicable for pattern matching of expressions or functions.
Here is the maven dependency for Apache commons lang StringUtils class of java api.
you can use this in your project pom.xml file as dependency.
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
The latest version of the dependency will be available here.
Stream Collectors
-
Now lets use Java Util Stream Collectors api package to convert List to String.
Here we leverage Java streams method stream() for conversion.
public static void main(String[] args) {
try {
List<Integer> list = Arrays.asList(1, 2, 3);
String result = list.stream().
map(i -> String.valueOf(i)).
collect(Collectors.joining("/", "(", ")"));
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
(1/2/3)
In above example we can see usage of stream().map , note that it is different from standard java map.
Now lets use Java Util Stream Collectors api package to convert List to String.
Here we leverage Java streams method stream() for conversion.
public static void main(String[] args) {
try {
List<Integer> list = Arrays.asList(1, 2, 3);
String result = list.stream().
map(i -> String.valueOf(i)).
collect(Collectors.joining("/", "(", ")"));
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
(1/2/3)
In above example we can see usage of stream().map , note that it is different from standard java map.
Comma Separator(delimiter)
-
Lets go through how to convert using comma separated values.
public static void main(String[] args) {
try {
List<String> countries = Arrays.asList("USA", "UK", "Australia", "India");
String countriesComma = String.join(",", countries);
System.out.println(countriesComma);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
USA,UK,Australia,India
As the per the output above, we can see conversion using delimiter i,e. separated by Comma or Comma separated.
Using join method, you can convert List to String with Separator comma, blackslash, space and so on.
Lets go through how to convert using comma separated values.
public static void main(String[] args) {
try {
List<String> countries = Arrays.asList("USA", "UK", "Australia", "India");
String countriesComma = String.join(",", countries);
System.out.println(countriesComma);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
USA,UK,Australia,India
As the per the output above, we can see conversion using delimiter i,e. separated by Comma or Comma separated.
Using join method, you can convert List to String with Separator comma, blackslash, space and so on.
Convert Character List to String
-
Lets go through how to convert List of Characters to String using StringBuilder class.
public static void main(String[] args) {
try {
List<Character> list =
Arrays.asList('c', 's', 'v');
StringBuilder sb = new StringBuilder();
for (Character chr : list) {
sb.append(chr);
}
// convert to string
String result = sb.toString();
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
csv
Lets go through how to convert List of Characters to String using StringBuilder class.
public static void main(String[] args) {
try {
List<Character> list =
Arrays.asList('c', 's', 'v');
StringBuilder sb = new StringBuilder();
for (Character chr : list) {
sb.append(chr);
}
// convert to string
String result = sb.toString();
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
csv
with Double Quotes
-
Using Apache Commons StringUtils package, convert List to String with Quotes using Java.
Refer the below implementation
public static void main(String[] args) {
try {
List<String> countries = Arrays.asList("USA", "UK", "Australia", "India");
String join = StringUtils.join(countries, "\", \"");
String wrapQuotes = StringUtils.wrap(join, "\"");
System.out.println(wrapQuotes);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
"USA", "UK", "Australia", "India"
Of course, you can convert using Single Quotes with single String as well.
Using Apache Commons StringUtils package, convert List to String with Quotes using Java.
Refer the below implementation
public static void main(String[] args) {
try {
List<String> countries = Arrays.asList("USA", "UK", "Australia", "India");
String join = StringUtils.join(countries, "\", \"");
String wrapQuotes = StringUtils.wrap(join, "\"");
System.out.println(wrapQuotes);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
"USA", "UK", "Australia", "India"
Of course, you can convert using Single Quotes with single String as well.
Sort Method
-
we can convert using Java Sort with below implementation.
public static void main(String[] args) {
try {
List<String> countries = Arrays.asList("USA", "UK", "Australia", "India");
countries.sort(Comparator.comparing(String::toString));
System.out.println(countries);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
[Australia, India, UK, USA]
we can convert using Java Sort with below implementation.
public static void main(String[] args) {
try {
List<String> countries = Arrays.asList("USA", "UK", "Australia", "India");
countries.sort(Comparator.comparing(String::toString));
System.out.println(countries);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
[Australia, India, UK, USA]
toJson Method
-
Lets convert List to String Json in Java using Google GSON library.
It is straight forward method ToJson() which will set and convert the input to Json String.
public static void main(String[] args) {
try {
List<String> countries = Arrays.asList("USA", "UK", "Australia", "India");
String json = new Gson().toJson(countries);
System.out.println(json);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
["USA","UK","Australia","India"]
Lets convert List to String Json in Java using Google GSON library.
It is straight forward method ToJson() which will set and convert the input to Json String.
public static void main(String[] args) {
try {
List<String> countries = Arrays.asList("USA", "UK", "Australia", "India");
String json = new Gson().toJson(countries);
System.out.println(json);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
["USA","UK","Australia","India"]
In Java 8
-
Lets convert List to String using String.join() method in Java 8 .
public static void main(String[] args) {
try {
List<String> list = Arrays.asList("USA", "UK", "INDIA");
String delimiter = "-";
String result = String.join(delimiter, list);
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
USA-UK-INDIA
Lets convert List to String using String.join() method in Java 8 .
public static void main(String[] args) {
try {
List<String> list = Arrays.asList("USA", "UK", "INDIA");
String delimiter = "-";
String result = String.join(delimiter, list);
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
USA-UK-INDIA
replaceAll Method
-
Lets convert List to String using replaceAll() and String.join() method.
You can also notice inline lambda expressions used in the replaceAll method.
public static void main(String[] args) {
try {
List<String> countries = Arrays.asList("usa", "uk", "india");
countries.replaceAll(r -> r.toUpperCase());
String result = String.join(" ", countries);
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
USA UK INDIA
Lets convert List to String using replaceAll() and String.join() method.
You can also notice inline lambda expressions used in the replaceAll method.
public static void main(String[] args) {
try {
List<String> countries = Arrays.asList("usa", "uk", "india");
countries.replaceAll(r -> r.toUpperCase());
String result = String.join(" ", countries);
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
USA UK INDIA
LinkedList to String
-
Lets convert LinkedList to String using String.join() method.
public static void main(String[] args) {
try {
LinkedList<String> list = new LinkedList();
list.add("USA");
list.add("UK");
list.add("INDIA");
String result = String.join(" ", list);
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
USA UK INDIA
In case of any issues during conversion, we can notice error message in the console log.
To conclude, in this tutorial we gone through different ways to convert a Java List to a String data types.
List to String methods conversion is also common in other programming languages like JavaScript, Python, Jquery.
In a nut shell, JavaScript uses str function for concatenate, likewise python program uses join method for conversion and concatenate a string.
Interested to read JavaScript resources, checkout this JavaScript split method article.
Keeping sharing java tutorials and happy coding 🙂
We hope this article helped you to find out list to string java. You may also want to see_
Lets convert LinkedList to String using String.join() method.
public static void main(String[] args) {
try {
LinkedList<String> list = new LinkedList();
list.add("USA");
list.add("UK");
list.add("INDIA");
String result = String.join(" ", list);
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
USA UK INDIA
In case of any issues during conversion, we can notice error message in the console log.
To conclude, in this tutorial we gone through different ways to convert a Java List to a String data types.
List to String methods conversion is also common in other programming languages like JavaScript, Python, Jquery.
In a nut shell, JavaScript uses str function for concatenate, likewise python program uses join method for conversion and concatenate a string.
Interested to read JavaScript resources, checkout this JavaScript split method article.
Keeping sharing java tutorials and happy coding 🙂
Tags: Java, java list of array,lists in java,list of arrays in java,collection java,list string java,list<> java
