How to Collect Entity IDs from Java Collections
In many applications, we often work with collections of domain entities retrieved from a database, API, or in-memory source, and a common requirement is to extract only the IDs from those entities to send them to another service, perform batch updates or deletions, build lookup maps, validate relationships, or support logging and auditing. This article demonstrates several methods for retrieving all IDs from a collection of entities using Java.
1. Sample Domain Model
Let’s start with a simple entity class.
public class Product {
private final Long id;
private final String name;
private final double price;
public Product(Long id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}
Sample Data Setup
Next, we’ll create a list of Products to be used in all examples.
public class SampleData {
public static List<Product> products() {
return List.of(
new Product(101L, "Laptop", 1200.00),
new Product(102L, "Phone", 800.00),
new Product(103L, "Tablet", 500.00)
);
}
}
2. Using Java Streams
This is the most concise and expressive approach in modern Java.
public class EntityIdExtractor {
public static void main(String[] args) {
List<Product> products = SampleData.products();
List<Long> ids = products.stream()
.map(Product::getId)
.collect(Collectors.toList());
System.out.println(ids);
}
}
The process starts by converting the collection into a stream using products.stream(), then each element is transformed into its identifier with .map(Product::getId), and finally, the resulting values are gathered into a new List<Long> using .collect(Collectors.toList()).
If you’re using Java 16 or later, you can simplify the code.
List<Long> ids = products.stream()
.map(Product::getId)
.toList();
This returns an immutable list.
3. Using a Traditional Loop
If you’re working in older Java versions or prefer explicit logic.
public class LoopExample {
public static void main(String[] args) {
List<Product> products = SampleData.products();
List<Long> ids = new ArrayList<>();
for (Product product : products) {
ids.add(product.getId());
}
System.out.println(ids);
}
}
This approach is easy to understand but more verbose.
4. Removing Duplicates Using a Set
If your collection may contain duplicate IDs, and you want unique values.
Set<Long> uniqueIds = products.stream()
.map(Product::getId)
.collect(Collectors.toSet());
System.out.println(uniqueIds);
Handling Null IDs Safely
In some systems, entities may not yet be persisted and may have null IDs.
List<Long> ids = products.stream()
.map(Product::getId)
.filter(id -> id != null)
.toList();
This prevents NullPointerException and ensures only valid IDs are collected.
5. Conclusion
In this article, we explored various ways to extract the collection of IDs from a set of entities in Java. We demonstrated how the Streams API enables concise and readable transformations, how to handle null or duplicate IDs, and how to collect results efficiently into lists or sets.
6. Download the Source Code
This article covered approaches for collecting all entity IDs from a collection in Java.
You can download the full source code of this example here: java collection ids entities

