Combining one object into another object

I have two objects of the same type. I want to create a method that can combine the properties of two objects and can return a single object.

For example: Consider a class ABC having 4 fields

Class ABC{
String name;
String id;
String salary;
String status;
}

Assume the first object

ABC[name=ZEE,id=2,salary=null,status=1]

and the second object

ABC[name=john,id=null,salary=12200,status=null]

I want to create a general method that can combine these two objects and can produce the result:

ABC[name=ZEE,id=2,salary=12200,status=1]

The method must take two parameters of the object type:

Object  mergeObject(Object Obj1, Object Obj2){
}

Note. This property will have the first property of the object if both objects have a nonzero value for this property.

+4
source share
4 answers

. , , . , . , .

@SuppressWarnings("unchecked")
public static <T> T mergeObjects(T first, T second) throws IllegalAccessException, InstantiationException {
    Class<?> clazz = first.getClass();
    Field[] fields = clazz.getDeclaredFields();
    Object returnValue = clazz.newInstance();
    for (Field field : fields) {
        field.setAccessible(true);
        Object value1 = field.get(first);
        Object value2 = field.get(second);
        Object value = (value1 != null) ? value1 : value2;
        field.set(returnValue, value);
    }
    return (T) returnValue;
}

public static class ABC {
    private int id;
    private String name;
    private int[] numbers;

    public ABC() {
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int[] getNumbers() {
        return numbers;
    }

    public void setNumbers(int[] numbers) {
        this.numbers = numbers;
    }
}

public static void main(String[] args) throws InstantiationException, IllegalAccessException {
    ABC abc = new ABC();
    abc.setId(1);
    abc.setName("Hello");
    int[] newnumbers = new int[5];
    for(int i = 0; i < newnumbers.length; i++) {
        newnumbers[i] = i;
    }
    abc.setNumbers(newnumbers);
    ABC abc2 = new ABC();
    abc2.setName("World");

    ABC abcFinal = mergeObjects(abc, abc2);
    System.out.println("Properties of ABC Final:");
    System.out.println("ID: " + abcFinal.getId());
    System.out.println("Name: " + abcFinal.getName());
    System.out.println("Numbers: " + Arrays.toString(abcFinal.getNumbers()));
}

:

Properties of ABC Final:
ID: 1
Name: Hello
Numbers: [0, 1, 2, 3, 4]
+9

:

public ABC mergeABC(ABC obj1, ABC obj2){
    ABC retVal = new ABC();
    retVal.name   = ((obj1.name   != null) ? obj1.name   : obj2.name   );
    retVal.id     = ((obj1.id     != null) ? obj1.id     : obj2.id     );
    retVal.salary = ((obj1.salary != null) ? obj1.salary : obj2.salary );
    retVal.status = ((obj1.status != null) ? obj1.status : obj2.status );
    return retVal;
}

" " obj1 obj2, .

+2

, .

-1

1 ( , ), :

private CustomObject mergeObject(CustomObject Obj1, CustomObject Obj2) {
    CustomObject tempObject = new CustomObject();
     If (Obj1.getName() == null) {
      tempObject.setName(Obj2.getName());
     } else {
         tempObject.setName(Obj1.getName());
     }
     return tempObject;
} 

if .

-1

All Articles