In many Java applications, especially when working with databases, APIs, or configuration files, it’s common to need a comma-separated list of IDs extracted from a List of objects. Whether you’re filtering results, generating SQL queries, or preparing data for front-end display, learning how to transform a List<MyObject> into a String of comma-separated IDs is a useful skill.
In this tutorial, we’ll walk through the best practices to convert a list of Java objects into a comma-separated string of their IDs using Java 8’s Stream API.
Why You Might Need a Comma-Separated List of IDs
Here are a few common use cases:
- Sending a list of selected IDs to a REST API.
- Constructing a SQL
INclause (SELECT * FROM table WHERE id IN (1,2,3)). - Displaying a summary of selected items in the UI.
- Generating CSV or log output.
Sample Java Class: MyObject
Assume you have a class named MyObject with a numeric or string-based ID:
public class MyObject {
private int id;
public MyObject(int id) {
this.id = id;
}
public int getId() {
return id;
}
}
Java 8+ Solution Using Streams
Here’s how you can easily extract the IDs and create a comma-separated String:
import java.util.List;
import java.util.stream.Collectors;
public class Example {
public static void main(String[] args) {
List<MyObject> objectList = List.of(
new MyObject(1),
new MyObject(2),
new MyObject(3)
);
String commaSeparatedIds = objectList.stream()
.map(MyObject::getId) // Extract ID
.map(String::valueOf) // Convert to String
.collect(Collectors.joining(",")); // Join with commas
System.out.println(commaSeparatedIds); // Output: 1,2,3
}
}
Explanation
.stream()converts the list into a stream..map(MyObject::getId)extracts the ID from each object..map(String::valueOf)ensures the ID is a string..collect(Collectors.joining(","))combines all elements into a single string, separated by commas.
Adapting for String IDs
If your IDs are already strings (e.g., UUIDs or codes), you can skip the String::valueOf step:
.map(MyObject::getId)
.collect(Collectors.joining(","))
Handling Nulls Safely
If your list might contain null objects or objects with null IDs, filter them out:
String safeCommaSeparatedIds = objectList.stream()
.filter(obj -> obj != null && obj.getId() != null)
.map(obj -> String.valueOf(obj.getId()))
.collect(Collectors.joining(","));
Using Java Before Streams (Pre-Java 8)
If you’re working with an older version of Java:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < objectList.size(); i++) {
sb.append(objectList.get(i).getId());
if (i < objectList.size() - 1) {
sb.append(",");
}
}
String result = sb.toString();
While more verbose, this method is fully compatible with Java 6 or 7.
Conclusion
Converting a list of Java objects to a comma-separated string of IDs is a common task with many practical applications. By leveraging Java 8’s Stream API, you can write concise, readable, and efficient code for this purpose.
Whether you’re building web applications, data processing services, or backend APIs, this pattern will help you manipulate and present object data in a format that’s easy to consume and transmit.


