title: Is there a way to compare lambdas?
body: <p>Say I have a List of object which were defined using lambda expressions (closures).  Is there a way to inspect them so they can be compared?</p>
  
  <p>The code I am most interested in is</p>
  
  <pre><code>    List&lt;Strategy&gt; strategies = getStrategies();
      Strategy a = (Strategy) this::a;
      if (strategies.contains(a)) { // ...
  </code></pre>
  
  <p>The full code is</p>
  
  <pre><code>import java.util.Arrays;
  import java.util.List;
  
  public class ClosureEqualsMain {
      interface Strategy {
          void invoke(/*args*/);
          default boolean equals(Object o) { // doesn't compile
              return Closures.equals(this, o);
          }
      }
  
      public void a() { }
      public void b() { }
      public void c() { }
  
      public List&lt;Strategy&gt; getStrategies() {
          return Arrays.asList(this::a, this::b, this::c);
      }
  
      private void testStrategies() {
          List&lt;Strategy&gt; strategies = getStrategies();
          System.out.println(strategies);
          Strategy a = (Strategy) this::a;
          // prints false
          System.out.println("strategies.contains(this::a) is " + strategies.contains(a));
      }
  
      public static void main(String... ignored) {
          new ClosureEqualsMain().testStrategies();
      }
  
      enum Closures {;
          public static &lt;Closure&gt; boolean equals(Closure c1, Closure c2) {
              // This doesn't compare the contents 
              // like others immutables e.g. String
              return c1.equals(c2);
          }
  
          public static &lt;Closure&gt; int hashCode(Closure c) {
              return // a hashCode which can detect duplicates for a Set&lt;Strategy&gt;
          }
  
          public static &lt;Closure&gt; String asString(Closure c) {
              return // something better than Object.toString();
          }
      }    
  
      public String toString() {
          return "my-ClosureEqualsMain";
      }
  }
  </code></pre>
  
  <p>It would appear the only solution is to define each lambda as a field and only use those fields. If you want to print out the method called, you are better off using <code>Method</code>. Is there a better way with lambda expressions?</p>
  
  <p>Also, is it possible to print a lambda and get something human readable?  If you print <code>this::a</code> instead of</p>
  
  <pre><code>ClosureEqualsMain$$Lambda$1/821270929@3f99bd52
  </code></pre>
  
  <p>get something like</p>
  
  <pre><code>ClosureEqualsMain.a()
  </code></pre>
  
  <p>or even use <code>this.toString</code> and the method.</p>
  
  <pre><code>my-ClosureEqualsMain.a();
  </code></pre>