Compare object fields of different classes in Java

后端 未结 3 2085
时光说笑
时光说笑 2021-01-23 21:02

I have two objects, each of which have tens of fields:

Class1 {
    int firstProperty;
    String secondProperty;
    ...
}

Class2 {
    int propertyOne;
    St         


        
3条回答
  •  情书的邮戳
    2021-01-23 21:59

    Comparing objects of different classes by specific fields is exactly the scenario that https://github.com/nejckorasa/compare-utils solves.

    Example:

    ObjectCmp.equalEqualityPairs(
        object1,
        object2,
        Arrays.asList(
            EqualityPair.of(o1 -> o1.getFirstProperty(), o2 -> o2.getPropertyOne()),
            EqualityPair.of(o1 -> o1.getSecondProperty(), o2 -> o2.getPropertyTwo())));
    

    Take a look, it might be useful!

提交回复
热议问题