In Java, working with collections like lists is fundamental. Often, you’ll need to convert objects or records into lists for various purposes. In this tutorial, we’ll explore different approaches to create a List from a property of an Array of Java objects or Records using practical examples.
From an Array of Java Objects to a List
Sometimes you will need to extract a single property from an Array of Java Objects and convert into a Collection such as the java.util.List
. In our first example, we will show two simple ways to achieve it by using just a one-liner and a bit of Lambda functional programming.
Firstly, let’s define the following Java Bean:
class CustomerDTO { private String username; private String password; public CustomerDTO(String username, String password) { this.username = username; this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
Then, let’s create an array of CustomerDTO
objects as follows:
CustomerDTO[] customersDTO = { new CustomerDTO("user1", "password1"), new CustomerDTO("user2", "password2"), new CustomerDTO("user3", "password3"), new CustomerDTO("user4", "password4"), new CustomerDTO("user5", "password5") };
In this following code snippet, we will show how to create a List from the array of CustomerDTO
using Java Streams. Specifically, the map
operation transforms objects into specific fields, followed by collecting them into lists using Collectors.toList()
.
List<String> list = Arrays.stream(customersDTO) .map(customer -> customer.getUsername()) .collect(Collectors.toList());
In the above code, the List object will contain all CustomerDTO
username attributes.
Please note that the above can also be rewritten in a slightly different format, without changing the result:
List<String> list2 = Arrays.asList(customersDTO) .stream() .map(CustomerDTO::getUsername) .collect(Collectors.toList());
From an Array of Records to a Java List
The second use case we will showcase is different as it uses a Java Record to store the Customer array of data:
record Customer(String username, String password) { // Additional methods or customizations can be added here if needed }
Thanks to the magic features of Java Record, you will get out of the box a Constructor with fields for the above Record. Therefore, you can create an Array of Customer records as follows:
Customer[] customers = { new Customer("user1", "password1"), new Customer("user2", "password2"), new Customer("user3", "password3"), new Customer("user4", "password4"), new Customer("user5", "password5") };
Then, in order to extract the array of username from the Record into a Java List
, you will need to change a bit our previous examples:
List<String> list3 = Arrays.stream(customers) .map(customer -> customer.username()) .collect(Collectors.toList()); List<String> list4 = Arrays.asList(customers) .stream() .map(Customer::username) .collect(Collectors.toList());
As you can see, you will not access the Java Record with the getUserName
anymore but using the built-in customer.username()
.
From a List of Records to another List of Records
Finally, we will show case how to copy a List of Java Records into another List of Java Records. Commonly, this use case can happen if you want to transform a complex Record into a smaller one. You can achieve it in multiple ways: here we will show how to copy some fields from a List of the CustomerFull
Record into the fields of a Customer
Record we already know.
Firstly, let’s add the new CustomerFull
Record:
record CustomerFull(String username, String password, String email, String address) { // Additional methods or customizations can be added here if needed }
Then, let’s init some elements of this Record with:
List<CustomerFull> customerFullList = List.of( new CustomerFull("user1", "pass1", "email1", "address1"), new CustomerFull("user2", "pass2", "email2", "address2") );
Finally, we will copy the List of CustomerFull
into a List of Customer
objects, picking up only the attributes that we’re interested in:
List<Customer> customerList = customerFullList.stream() .map(full -> new Customer(full.username(), full.password())) .collect(Collectors.toList());
Conclusion
In summary, converting an array of Java objects or records into a list is a common task in Java programming. This article has explored several efficient approaches to achieve this conversion using Lambda expressions