• Register
0 votes

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:



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?

6 5 3
7,540 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;



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))



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




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.

9 7 4
38,600 points
0 votes


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.


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.

10 6 4
31,120 points

Related questions

0 votes
1 answer 3 views
Problem: I am in trouble while solving it .. please help me .. illegal attempt to map a non collection as a @onetomany, @manytomany or @collectionofelements
asked 3 days ago Ifra 24.4k points
0 votes
2 answers 1.8K views
Problem : I have some objects in the following configuration: · A has the many-to-many relationship with B. (B has inverse="true") · B has the many-to-one relationship with C. (I have cascade set to "save-update") · C is the kind of type or ... that for architectural reasons beyond my control each read or write needs to be done in a separate session. Does anybody has any solution on my issue?
asked Nov 27, 2019 alecxe 7.5k points
0 votes
1 answer 35 views
Problem: Hi there! I want to know the difference between the Collections and Collection in Java. What is the main difference between both, and can they be used interchangeably? Please answer in detail. Thanks
asked Dec 29, 2020 Code Learner 9.2k points
0 votes
1 answer 3 views
Problem: I need someone to understand this problem and find out the solution. what is the next two letters in the sequence: a - e - f - h - i - k - l - m - &hellip;
asked 4 days ago Ifra 24.4k points
0 votes
1 answer 16 views
Problem: count occurrences of character in string java using hashmap.
asked Feb 18 Ethan ross 2.3k points
0 votes
3 answers 6 views
Problem: I am stuck with this ... Please help me that how to deal with this? how to sort a set in java
asked Apr 4 Ifra 24.4k points
0 votes
1 answer 3 views
Problem: How to sort a set in java. can somebody help me to get this one.
asked Mar 21 charles mathews 5.5k points
0 votes
1 answer 18 views
Problem: HELP! I need help understanding this error: Error creating bean with name 'entitymanagerfactory' defined in class path resource.
asked Mar 6 Wafa Abu Yousef 6.1k points
1 vote
1 answer 4.5K views
Problem : When I try to compile my spring project, I always face the below error : Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed I have the ... >1.4.1.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> . . .
asked Dec 12, 2019 alecxe 7.5k points
0 votes
1 answer 10 views
Problem: I need help in solving this problem: The internal connection pool has reached its maximum size and no connection is currently available!
asked Mar 14 Wafa Abu Yousef 6.1k points