GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Java List to String - Converting a List to a String in Java Java List to StringLast modified October 13, 2026 Java 25
This tutorial explains different techniques to convert a
The examples use compact source files with an instance Using toString
Every list has a Main.java
void main() {
List<String> fruits = List.of("apple", "banana", "orange");
List<Integer> numbers = List.of(1, 2, 3, 4);
List<List<Integer>> nested = List.of(List.of(1, 2), List.of(3, 4));
System.out.println(fruits.toString());
System.out.println(numbers);
System.out.println(nested);
System.out.println("Fruits: " + fruits);
}
The $ java Main.java [apple, banana, orange] [1, 2, 3, 4] [[1, 2], [3, 4]] Fruits: [apple, banana, orange] Using String.join
The Main.java
void main() {
List<String> words = List.of("a", "visit", "to", "London");
String result = String.join("-", words);
System.out.println(result);
String path = String.join("/", "home", "user", "docs");
System.out.println(path);
}
The first call creates a hyphen-separated string from the list. The second
joins its varargs arguments with a slash. Note that $ java Main.java a-visit-to-London home/user/docs Using Collectors.joining
The Main.java
import java.util.stream.Collectors;
void main() {
List<String> words = List.of("There", "are", "three", "chairs");
String sentence = words.stream()
.collect(Collectors.joining(" "));
System.out.println(sentence);
String upper = words.stream()
.map(String::toUpperCase)
.collect(Collectors.joining(", ", "{", "}"));
System.out.println(upper);
String concatenated = words.stream()
.collect(Collectors.joining());
System.out.println(concatenated);
}
The first stream joins the words with a space. The second converts them to uppercase and wraps the result in curly brackets. The third concatenates the words without any delimiter. $ java Main.java
There are three chairs
{THERE, ARE, THREE, CHAIRS}
Therearethreechairs
Converting lists of non-strings
To join a list of numbers or other objects, we first map each element to a
string with the stream Main.java
import java.util.stream.Collectors;
record User(String name, int age) {}
void main() {
List<Integer> numbers = List.of(1, 2, 3, 4);
String csv = numbers.stream()
.map(String::valueOf)
.collect(Collectors.joining(","));
System.out.println(csv);
List<User> users = List.of(new User("John", 34), new User("Roger", 25),
new User("Paula", 41));
String names = users.stream()
.map(User::name)
.collect(Collectors.joining(", "));
System.out.println(names);
String details = users.stream()
.map(u -> "%s (%d)".formatted(u.name(), u.age()))
.collect(Collectors.joining("; "));
System.out.println(details);
}
We convert a list of integers to a comma-separated string. Then we join the names of users, and finally we format each user with its name and age. $ java Main.java 1,2,3,4 John, Roger, Paula John (34); Roger (25); Paula (41) Using StringJoiner
The Main.java
void main() {
List<String> fruits = List.of("apple", "banana", "orange");
var joiner = new StringJoiner(", ", "[", "]");
fruits.forEach(joiner::add);
System.out.println(joiner);
var empty = new StringJoiner(", ", "[", "]");
empty.setEmptyValue("no fruits");
System.out.println(empty);
}
The first joiner separates the elements with a comma and wraps the result in
square brackets. The $ java Main.java [apple, banana, orange] no fruits Using StringBuilder
Main.java
void main() {
List<String> words = List.of("There", "are", "three", "chairs");
var builder = new StringBuilder();
for (String word : words) {
if (!builder.isEmpty()) {
builder.append(' ');
}
builder.append(word);
}
String result = builder.toString();
System.out.println(result);
}
A space is appended before every word except the first. This avoids a trailing
delimiter. Removing it afterwards with $ java Main.java There are three chairs String concatenation in a loop
We can also concatenate the elements with the Main.java
void main() {
List<String> words = List.of("one", "two", "three");
String result = "";
for (int i = 0; i < words.size(); i++) {
result += words.get(i);
if (i < words.size() - 1) {
result += ", ";
}
}
System.out.println(result);
}
The example concatenates the elements of a list into a comma-separated string. The delimiter is not added after the last element. $ java Main.java one, two, three Handling null elements
Lists such as Main.java
import java.util.stream.Collectors;
void main() {
List<String> words = Arrays.asList("sky", null, "blue");
System.out.println(words);
System.out.println(String.join(", ", words));
String filtered = words.stream()
.filter(Objects::nonNull)
.collect(Collectors.joining(", "));
System.out.println(filtered);
String replaced = words.stream()
.map(w -> Objects.requireNonNullElse(w, "?"))
.collect(Collectors.joining(", "));
System.out.println(replaced);
}
The $ java Main.java [sky, null, blue] sky, null, blue sky, blue sky, ?, blue Which method to use
Avoid SourceJava String.join documentation
This tutorial has demonstrated various methods to convert a AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Java tutorials. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |