I have some code which looks like the below:
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
class MyObj {
private final Double aDouble;
public MyObj(Double aDouble) {
this.aDouble = aDouble;
}
}
class Main {
public static void main(String[] args) {
List<Function<MyObj, String>> functionList1 = new ArrayList<>();
List<Function<MyObj, String>> functionList2 = new ArrayList<>();
// ... Add same Function<MyObj, String>s to both lists
// I want to assert that functionList1.equals functionList2
}
}
I'd like to check that some Function
, Supplier
, BiFunction
or whatever it might be of MyObj
, would be equal to another if the result of calling the Function
/Supplier
etc returns the same value given the same input.
So in this case, Java would compare the values of the two lists using equals
like so functionList1.get(0).apply(standardInstanceOfMyObj)
equals functionList2.get(0).apply(standardInstanceOfMyObj)
etc.
My question is, how can I override equals
and hashcode
for a specific type like Function<MyObj, String>
to make the above work?