java - Checking equals for JAXB object considering some of the fields -
my autogenerated jaxb class
public class employee { private int id; private string name; private string department; /* getters , setters */ } employee emp1 = new employee(); emp1.setid(1); emp1.setname("a"); emp1.setdepartment("d1"); employee emp2 = new employee(); emp2.setid(2); emp2.setname("b"); emp2.setdepartment("d1"); list<employee> emplist1 = new arraylist<employee>(); emplist1.add(emp1); emplist2.add(emp2); employee emp3 = new employee(); emp2.setid(3); emp2.setname("a"); emp2.setdepartment("d1"); list<employee> emplist2 = new arraylist<employee>(); emplist2.add(emp3);
i want compare both list emplist1 , emplist2 , result list matches name , department fields of employee object.
basically, need intersection of 2 list based on custom comparison of fields in 2 objects.
i looking @ google guava , lambdaj not able find solution/sample doing intersection custom comparison logic.
any idea helpful.
for comparing can use guava ordering or java comparator. , filtering can use predicate.
if want general can use make 1 comparator(for both dep , name)
class mycomparator implements comparator<employee> { @override public int compare(final employee left, final employee right) { return comparisonchain.start().compare(left.getname(), right.getname()) .compare(left.getdepartment(), right.getdepartment()).result(); } } class mypredicate implements predicate<employee> { final list<employee> employees; final comparator<employee> comparator; public mypredicate(final list<employee> employees, final comparator<employee> comparator) { this.employees = employees; this.comparator = comparator; } @override public boolean apply(@nullable final employee input) { (final employee e : employees) { if (comparator.compare(e, input) == 0) { return true; } } return false; } }
then u can use this:
final mycomparator comparator = new mycomparator(); final mypredicate predicate1 = new mypredicate(emplist1, comparator); final mypredicate predicate2 = new mypredicate(emplist2, comparator); system.out.println("####"); system.out.println(collections2.filter(emplist2, predicate1)); system.out.println(collections2.filter(emplist1, predicate2));
Comments
Post a Comment