title: java enum including a function - use switch or supplier as member variable
body: <p>I want to have an enum that provides a function that includes a constructor of a new class. Before Java8 I used to write this code:</p>
  
  <pre><code>enum Fruit1 {
      APPLE, BANANA;
  
      public Tree getTree() {
          switch (this) {
              case APPLE:
                  return new AppleTree();
              case BANANA:
                  return new BananaTree();
              default:
                  throw new IllegalStateException();
          }
      }
  }
  </code></pre>
  
  <p>Now, I can use lambda expressions. The advantage is, when adding a new enum entry, you cannot forget adding sth to the switch statement. But I dont like that the code looks more complicated to me.</p>
  
  <pre><code>enum Fruit2 {
      APPLE(AppleTree::new), BANANA(BananaTree::new);
  
      Supplier&lt;Tree&gt; treeSupplier;
  
      Fruit2(Supplier&lt;Tree&gt; treeSupplier) {
          this.treeSupplier = treeSupplier;
      }
  
      public Tree getTree() {
          return treeSupplier.get();
      }
  }
  </code></pre>
  
  <p>Is it just a matter of taste or is there more to it? Can I do it in a nicer way using lambda expressions?</p>
  
  <p>This is strongly related to <a href="https://stackoverflow.com/questions/28277562/is-there-any-reason-to-override-methods-in-enums-in-java-8">other questions comparing lambda expressions to abstract methods</a>, but here I would like to focus on the switch expression, that I am still quite comfortable with (in comparison to lambda expressions).</p>