• 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
2 answers 1K 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 14 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 5.2k points
0 votes
1 answer 1.5K views
Problem : I am using the Hibernate for a first time for my project and I am very new to Hibernate. I think I have followed all the instructions written in some tutorials but still I keep getting the below Exception that is in my title: Exception in thread "main" ... should just change a connection.pool_size property, but if I do so it guesses that I only will get more errors. Can anyone help me?
asked Jan 30, 2020 jwilliam 3.9k points
1 vote
1 answer 2.6K 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
2 answers 662 views
Problem : I am facing following issue need solution return true if the string "cat" and "dog" appear the same number of times in the given string
asked Nov 13, 2019 peterlaw 6.9k points
0 votes
1 answer 52 views
I am new to this. I was doing that but I got into some problems.
asked Nov 6, 2020 TeamScript 9.2k points
0 votes
1 answer 42 views
Problem: I am working on Hibernate and now I am facing what I believe is a simple problem with the Hibernate but still I can't solve it. I have written a simple class I would like to persist but still keep facing below error: SEVERE: Field 'id' doesn't have a ... that is not working for me. Initializing the id also did not help me either! I am looking for Hibernate expert to fix my above error.
asked Jul 30, 2020 Raphael Pacheco 4.9k points
0 votes
2 answers 116 views
Problem : I am facing the following error when I save the object using Hibernate: object references an unsaved transient instance - save the transient instance before flushing:
asked Nov 14, 2019 peterlaw 6.9k points
0 votes
1 answer 150 views
Problem: I am very new with Hibernate Annotations and I have already encountered a problem I hope that someone here can help with. I have two entities mainly Section and ScopeTopic. The section entity has the List class member so the One to Many relationship. If I try to ... I think it must be my own lack of understanding which is at fault so some of the guidance from experts here would be great.
asked Aug 31, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 177 views