Convert one/ heterogeneous object into another – stream lambda (java8 /example)

  • Given two heterogeneous objects, we would like to convert one object into another object.
    • Transform one object into another object using stream lambda (java 8).
  • Suppose,
    1. We are storing address attributes in DBAddress class
    2. And We are storing User attributes in DBUser object.
  • We would like to perform following operations.
    1. Convert DBAddress object to Address object.
    2. Convert DBUser object to User Object.

Program – convert /translate one object into another(lambda stream java8)

We would like to convert DBAddress/DBUser objects to corresponding Address/User objects respectively.

In main method, we are performing following operations.

  1. We will call getDBUserList to simulate database operation.
    • DBUsers class composes DBAddress class.
    • We will simulate object retrieval operation from database.
  2. We will print the attributes of DBUser and DBAddress objects.
  3. We will use lambda stream (java8) to convert heterogeneous objects.
    1. Convert DBAddress object to Address object.
    2. Convert DBUser object to User Object.
  4. At-last, we will print the converted or translated objects i.e. Address & User objects.
package org.learn;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/****** Database clases ********/
class DBAddress {
 public String street;
 public String locality;
 public String area;
 public String pinCode;

 public DBAddress(String street, String locality,String area, String pinCode) {
  this.street = street;
  this.locality = locality;
  this.area = area;
  this.pinCode = pinCode;
 }

 public String toString() {
  return street + " " + locality + " " + area + " " + pinCode;
 }
}

class DBUser {
 public String firstName;
 public String lastName;
 public String age;
 public DBAddress address;
 public String contact;
 public String role;

 public DBUser(String firstName, String lastName, String age, 
     DBAddress address, String contact, String role) {
  this.firstName = firstName;
  this.lastName = lastName;
  this.age = age;
  this.address = address;
  this.contact = contact;
  this.role = role;
 }

 public String toString() {
  return "[DbUser: " + firstName + " " + lastName +
    " [address: " + address.toString() + "] " + contact
    + " " + role + "]";
 }
}

/************ Domain Classes ****************************/
class Address {
 public String street;
 public String locality;
 public String area;
 public String pinCode;

 public Address(String street, String locality,String area, String pinCode) {
  this.street = street;
  this.locality = locality;
  this.area = area;
  this.pinCode = pinCode;
 }

 public String toString() {
  return " " + street + " " + locality + " " + area + " " + pinCode;
 }
}

class User {
 public String name;
 public String age;
 public Address address;
 public String contact;

 public User(String name, String age, Address address, String contact) {
  this.name = name;
  this.age = age;
  this.address = address;
  this.contact = contact;
 }

 public String toString() {
  return "[User: " + name + " " + age + " "
    + " [address: " + address.toString() + "] " + contact + "]";
 }
}

/*****************************************************/

public class ObjectTranslator {
 // Suppose we are getting this from database
 private static List getDBUserList() {
  List dbUserList = new ArrayList();
  // First user
  DBAddress dbAddress1 = new DBAddress
      ("Street hw", "Windsor", "Windsor", "112211");
  DBUser dbUser1 = new DBUser
    ("Mike", "Cook", "55", dbAddress1, "001225365", "ViewOnly");
  dbUserList.add(dbUser1);
  // Second user
  DBAddress dbAddress2 = new DBAddress
    ("Street sw", "Hudson park", "Hudson", "221102");
  DBUser dbUser2 = new DBUser
    ("Peter", "thomas", "41", dbAddress2, "003665412", "Admin");
  dbUserList.add(dbUser2);
  return dbUserList;
 }

 public static void main(String[] args) {
  List dbUserList = getDBUserList();

  dbUserList.forEach(db -> System.out.println(db));
  List userList = dbUserList.stream().map(db -> {
   DBAddress dbAddress = db.address;
   Address address = new Address(dbAddress.street, dbAddress.locality,
     dbAddress.area, dbAddress.pinCode);
   User user = new User(db.firstName, db.age, address, db.contact);
   return user;
  }).collect(Collectors.toList());
  System.out.println("\nObject translated DBUser to User : ");
  userList.forEach(user -> System.out.println(user));
 }
}

Output – convert /translate one object into another (lambda stream java8)

[DbUser: Mike Cook [address: Street hw Windsor Windsor 112211] 001225365 ViewOnly]
[DbUser: Peter thomas [address: Street sw Hudson park Hudson 221102] 003665412 Admin]

Object translated DBUser to User :
[User: Mike 55 [address: Street hw Windsor Windsor 112211] 001225365]
[User: Peter 41 [address: Street sw Hudson park Hudson 221102] 003665412]
Scroll to Top