I have two DTO objects like following, please, note that I am using lombok to avoid boilerplate code.
DtoA
import lombok.Data;
import java.util.List;
@Data
public class DtoA {
private String name;
private String number;
private List<String> aList;
}
DtoB
import lombok.Data;
import java.util.List;
@Data
public class DtoB {
private String name;
private String phone;
private List<String> bList;
}
I want to compare specific fields of both objects so i have created an adaptor kind of object like following
DtoAdapter
import lombok.Data;
import java.util.List;
@Data
public class DtoAdapter {
private String nameText;
private List<String> xList;
}
Following is my Test class with main method where i am trying to do comparison This comparison is failing because of aList and bList contains strings in different orders. I want to compare the contents of the list without worrying about their order.
Test
import junit.framework.Assert;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
DtoA a = new DtoA();
List<String> aList = new ArrayList<>();
aList.add("x"); aList.add("y"); aList.add("z");
a.setName("abc"); a.setNumber("123"); a.setAList(aList);
DtoB b = new DtoB();
List<String> bList = new ArrayList<>();
bList.add("z"); bList.add("x"); bList.add("y");
b.setName("abc"); b.setPhone("123"); b.setBList(bList);
DtoAdapter a1 = new DtoAdapter();
a1.setNameText(a.getName()); a1.setXList(a.getAList());
DtoAdapter b1 = new DtoAdapter();
b1.setNameText(b.getName()); b1.setXList(b.getBList());
// comparision failing because of lists contains string in different orders
Assert.assertEquals(a1, b1);
}
}
Note: I have tried writing compareTo (by implementing comparable interface into DtoAdapter class) But I couldn't write comparison of two lists with compareTo method like following
DtoAdapter with comparable interface
import lombok.Data;
import java.util.List;
@Data
public class DtoAdapter implements Comparable<DtoAdapter>{
private String nameText;
private List<String> xList;
@Override
public int compareTo(DtoAdapter o) {
return this.getNameText().compareTo(o.getNameText());
// how to compare this.getXList() and o.getXList() with compareTo?
}
}