Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Hashcode and Equals for Java 8 functional interface objects

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?

like image 949
two_stacks_one_heap Avatar asked Oct 24 '17 14:10

two_stacks_one_heap


People also ask

Can functional interface have equals method?

Answer. Yes, Comparator is a functional interface. The equals is an abstract method overriding one of the public methods of java. lang.

Can Java 8 default methods override equals hashCode and toString?

Three of the Object methods cannot have default methods for the reasons given above by Brian Goetz: equals(Object) , hashCode() , and toString() .

What is the hashCode () and equals () function?

Java hashCode() An object hash code value can change in multiple executions of the same application. If two objects are equal according to equals() method, then their hash code must be same. If two objects are unequal according to equals() method, their hash code are not required to be different.

Can we override equals method in functional interface?

Object . Unfortunately, an interface can not enforce its implementation classes to override these methods, but that shouldn't stop it from declaring it to document the contract.


1 Answers

You can't. You can, however, override them for any class of yours that does actually implement Function. Comparing functions (mathematically) is tricky business since the domain space might be infinite, so Java would have no way to know that two functions are the same (other than in the case of numerical identity, where equals() will be true anyway). If you have some specific functions for which you can provide a more fine-grained equals()/hashCode() (for example, because they are based on some parsed expression language and you can compare the string representations), then you'll have to write those criteria in your own class.

like image 141
Piotr Wilkin Avatar answered Oct 19 '22 22:10

Piotr Wilkin