2

How can I check if 2 lambda's run the same code? Suppose I have the following code:

main() {
    Predicate<Person> a = (person -> person.getAge() > 18);
    Predicate<Person> b = (person -> person.getAge() > 18);
    boolean x = equalLamdbaCode(a, b)); // should return true

    Predicate<Person> c = (person -> person.getAge() > 40);
    boolean y = equalLamdbaCode(a, c)); // should return false
}

Is there a reliable way to implement that equalLamdbaCode(a, b) method?

Geoffrey De Smet
  • 26,223
  • 11
  • 73
  • 120
  • what do you mean by comparing same code? `equals` or `==` is the comparisons I know ... can you clarify? – LeTex Mar 24 '21 at 19:04
  • 1
    The questions in the duplicate list [here](https://stackoverflow.com/q/33039002/12323248) state that this is not specified by JLS or JVMS, so implementations of Java have flexibility for lambdas. – akuzminykh Mar 24 '21 at 19:31
  • 2
    Does this answer your question? [Is there a way to compare lambdas?](https://stackoverflow.com/questions/24095875/is-there-a-way-to-compare-lambdas) – Flame239 Mar 24 '21 at 19:40

0 Answers0