title: Get Enum constant by value
body: <p>I want to create a method, which returns enum constants by it's value.</p>
  <p>I have multiple enum classes which looks like this one (some of them have getter variables with different names):</p>
  <pre><code>@AllArgsConstructor
  public enum PhoneType {
  
  MOBILE(&quot;Mobile&quot;, 3),
  HOME(&quot;Home&quot;, 6),
  WORK(&quot;Work&quot;, 7);
  
  @Getter
  String type;
  
  @Getter
  int id;
  }
  </code></pre>
  <p>I used this stream to get the enum constant:</p>
  <pre><code>int phoneTypeId = 3;
  
  PhoneType phoneType = Arrays.stream(PhoneType.values())
     .filter(p -&gt; p.getId() == phoneTypeId)
     .findFirst()
     .orElseThrow(() -&gt; new RuntimeException(&quot;Not able to find Enum....&quot;));
  
  System.out.println(phoneType.getType());
  </code></pre>
  <p><em>output is: &quot;Mobile&quot;</em></p>
  <p>Now I want to create a method which will work for different enum classes. I started with something like this, but I don't know how to rewrite the filter line, to make it work for any enum class. It would be ideal to pass this <em>&quot;p -&gt; p.getId() == phoneTypeId&quot;</em> as input parameter to this method. Any ideas?</p>
  <pre><code>public static &lt;E extends Enum&lt;?&gt;&gt; E getEnumByValue(Class&lt;E&gt; enumClass) {
      return Arrays.stream(enumClass.getEnumConstants())
              .filter(p -&gt; p.getId() == phoneTypeId)
              .findFirst()
              .orElseThrow(() -&gt; new RuntimeException(&quot;Not able to find Enum....&quot;));
  }
  </code></pre>
  <p>(I know that it's possible to do that if I implement interface for all enum classes, but then enum variables would have to have the same names.)</p>