Java Stream API - Convert List of objects to another List of objects using Java streams?
Table of contents
No headings in the article.
To convert a List of objects to another List of objects using Java streams, you can use the map()
method of the Stream
interface.
Assuming you have a List
of objects of type A
and you want to convert it to a List
of objects of type B
, you can follow these steps:
Create a
Stream
from the originalList
using thestream()
method.Use the
map()
method to transform each element of the stream from typeA
to typeB
.Collect the resulting
Stream
into aList
using thecollect()
method.
Here's an example code snippet:
List<A> originalList = ...; // your original list of type A
List<B> transformedList = originalList.stream()
.map(a -> /* transformation logic to convert type A to type B */)
.collect(Collectors.toList());
In the map()
method, you need to provide a lambda expression that takes an object of type A
as input and returns an object of type B
. This lambda expression should contain the logic to convert the input object of type A
to an output object of type B
.
Note that you'll need to replace the ...
with your actual list of objects of type A
and also provide the transformation logic inside the lambda expression to convert from type A
to type B
.
here's an example of how to convert a List
of Address
objects to a List
of OfficeAddress
objects using Java streams:
Suppose you have an Address
class defined as follows:
public class Address {
private String street;
private String city;
private String state;
private String zipCode;
// Constructor, getters, and setters omitted for brevity
}
Now office address
public class OfficeAddress {
private String officeNumber;
private String street;
private String city;
private String state;
private String zipCode;
// Constructor, getters, and setters omitted for brevity
}
Now converting one List to another:
List<Address> addresses = ...; // your original list of Address objects
List<OfficeAddress> officeAddresses = addresses.stream()
.map(address -> new OfficeAddress("123", address.getStreet(), address.getCity(), address.getState(), address.getZipCode()))
.collect(Collectors.toList());
In this code, we're using the map()
method to transform each Address
object in the original list to an OfficeAddress
object. The lambda expression inside the map()
method takes an Address
object as input and returns an OfficeAddress
object. We're passing "123"
as the office number for all the OfficeAddress
objects since it's not available in the original Address
objects.
Note that you'll need to replace the ...
with your actual list of Address
objects. Also, if you have different requirements for constructing OfficeAddress
objects, you can modify the lambda expression inside the map()
method accordingly.
I hope this helps, you!!
More such articles:
https://www.youtube.com/channel/UCiTaHm1AYqMS4F4L9zyO7qA
\==========================**=========================
If this article adds any value to you then please clap and comment.
Let’s connect on Stackoverflow, LinkedIn, & Twitter.