Sometimes we need to convert or cast or transform an object from one type to another class. There are many ways, by which we can do it.
Let's say we have two objects ExternalPeopleInfo and InternalPeopleInfo. Assume we get ExternalPeopleInfo by calling some external API, but to save the details in our system we need to convert it in to InternalPeopleInfo.
Solution 1). We can write a constructor in InternalPeopleInfo, which takes ExternalPeopleInfo object from which it get the desired values and set it in InternalPeopleInfo object.
Solution 2). We can use Java 8's java.util.function.Function, which accepts one argument and produces a result.
To convert an object into another we need to define a function, which returns the required object (InternalPeopleInfo) and take the object which need to be transformed (ExternalPeopleInfo).
-K Himaanshu Shuklaa..
Let's say we have two objects ExternalPeopleInfo and InternalPeopleInfo. Assume we get ExternalPeopleInfo by calling some external API, but to save the details in our system we need to convert it in to InternalPeopleInfo.
Solution 1). We can write a constructor in InternalPeopleInfo, which takes ExternalPeopleInfo object from which it get the desired values and set it in InternalPeopleInfo object.
Solution 2). We can use Java 8's java.util.function.Function, which accepts one argument and produces a result.
To convert an object into another we need to define a function, which returns the required object (InternalPeopleInfo) and take the object which need to be transformed (ExternalPeopleInfo).
-K Himaanshu Shuklaa..
No comments:
Post a Comment