I need to obtain a set difference between two sets/lists but the objects in these sets are constructed differently. I would like the set difference be calculated on only one field of these objects, how can I do this?
POJO o1 = new POJO();
o1.setName("Bean");
o1.setLocation("Boston");
POJO o2 = new POJO();
o2.setName("Bagel");
o2.setLocation("NYC");
POJO o3 = new POJO();
o3.setName("Bean");
List<POJO> p1 = new ArrayList<>();
p1.add(o1);
p1.add(o2);
List<POJO> p2 = new ArrayList<>();
p2.add(o3);
Collection<POJO> subList = CollectionUtils.subtract(p1, p2);
System.out.println("subList: "+subList.toString());
The output I see is : subList: [POJO(name=Bean)]
i.e. o3
. But for my use case, o1
and o3
are the same - how can I achieve this? I overrode the default equals()
method as well but i don't think it is invoked.