• Register
0 votes
186 views

Problem :

Here I want to copy all the objects in the List with object1 to a list with object2:

Following is my code snip:

List<EmployeeAmManegmentModel> AM = employee.amEmployeeDeatails();
List<EmployeeRmManegmentModel> RM = employee.rmEmployeeDeatails();  
List<EmployeeGenericModel> GM = new ArrayList<>();

I want to copy AM and RM to GM. All 3 models have the same parameters:

 

MyEmployeeGenericModel

private Long id;
private String name;
private Long contactNumber;
private String emailId;
private Long status;
private String employeeType;

How can I copy the result from one list of different object type to another?

7.5k points

Please log in or register to answer this question.

2 Answers

0 votes

Solution :

If the EmployeeAMMenegement and the EMplyeeRMManagement model is the same class with some different methods then you can use the extend EmployeeGenericModel from the both class.

But you need do this only by creating mapper from AM, or RM class as follows:

public class EmployeeAmManegmentModel {
  public EmployeeAmMenegementModel(EmployeeGenericModel egm){
    this.id = egm.id;
    this.name = egm.name;
    //dst
  }
}

 

OR

If I have correctly understood your task, you can do something as follows:

    Gson gson = new Gson();

    List<EmployeeGenericModel> GM = am.stream()

                    .map(x -> gson.fromJson(gson.toJson(x), EmployeeGenericModel.class))

                    .collect(Collectors.toList());

    GM.addAll(rm.stream()

                    .map(x -> gson.fromJson(gson.toJson(x), EmployeeGenericModel.class))

                    .collect(Collectors.toList());

 

OR

There is no completely automatic way to do the copying. But, there are libraries for mapping objects of different classes onto each other.

The library with very comprehensive features for mapping is Dozer.

38.6k points
0 votes

Solution:

The Correct Answer of this question is Write a method for the class that will make a field by field copy of object 1 data members into object 2 data members.

Explanation:

You could overload and execute your assignment operator. They have formerly been instantiated, hence employing a copy constructor is too late I suppose.

Any of 2 methods can be employed a Deep copy or a Shallow copy

1) Shallow copy - This create copies of all the primordial data type variables however it creates reference to the pointer variable. It implies kit doesn't copy them it only makes a new reference to that similar address.The default copy constructor and assignment operator create shallow copies.

2) Deep copy - A deep copy copies all member variables, and creates copies of dynamically allocated memory pointed to by the variables. To create a deep copy, we have to identify our copy constructor and overload the assignment operator.

Need for deep copy in the class :

• A destructor is necessary to delete the dynamically allocated memory.

• A copy constructor is necessary to create a copy of the dynamically allocated memory.

• An overloaded assignment operator is necessary to create a copy of the dynamically allocated memory.

31.7k points
edited by