title: Finding enum value with Java 8 Stream API
body: <p>Suppose there is a simple enum called Type defined like this:</p>
  
  <pre><code>enum Type{
      X("S1"),
      Y("S2");
  
      private String s;
  
      private Type(String s) {
          this.s = s;
      }
  }
  </code></pre>
  
  <p>Finding the correct enum for given <code>s</code> is trivially done with static method with for-loop (assume the method is defined inside enum), e.g.:</p>
  
  <pre><code>private static Type find(String val) {
          for (Type e : Type.values()) {
              if (e.s.equals(val))
                  return e;
          }
          throw new IllegalStateException(String.format("Unsupported type %s.", val));
  }
  </code></pre>
  
  <p>I think the functional equivalent of this expressed with Stream API would be something like this:</p>
  
  <pre><code>private static Type find(String val) {
       return Arrays.stream(Type.values())
              .filter(e -&gt; e.s.equals(val))
              .reduce((t1, t2) -&gt; t1)
              .orElseThrow(() -&gt; {throw new IllegalStateException(String.format("Unsupported type %s.", val));});
  }
  </code></pre>
  
  <p>How could we write this better and simpler? This code feels coerced and not very clear. The <code>reduce()</code> especially seems clunky and abused as it doesn't accumulate anything, performs no calculation and always simply returns <code>t1</code> (provided the filter returns one value - if it doesn't that's clearly a disaster), not to mention <code>t2</code> is there superfluous and confusing. Yet I couldn't find anything in Stream API that simply somehow returns directly a <code>T</code> from a <code>Stream&lt;T&gt;</code>.</p>
  
  <p>Is there a better way?</p>