Hello I have 2 lists that contains the same objects. I would like to perform any operation like intercesct, union, distinct by using predicate because I am unable to use equals to comparision.
Example:
class Car{
public String id;
public String color;
public int hashcode(){
//id field is used for hashcode
}
public boolean equals(){
//id field is used for equals
}
}
Now I have two lists of Cars. I need to find duplicates in this lists but not by id only by color.
List<Car> carList1 = new ArrayList(){ new Car(1,blue), new Car(2,green)};
List<Car> carList2 = new ArrayList(){ new Car(1,silver), new Car(4,green)};
I need to find second object from carList1 (new Car(2,green))
List Something similar to
Collection.intersect(carList1,carList2,comparator).
In C# I would use for it LINQ.
You can do similar think using Guava.
1) intersect is operation on sets, not on lists. So you should construct them like
or, if you need special comparator ( predicate mentioned )
UPD: You should use only one way to construct both sets.
Than call intersection