title: Filter Array of cookies for a specific cookie and get value of a specific cookie in Java
body: <p>I am fixing some issue in a legacy module and came across this code which get the array of cookies from request object which can return null. The code looks for a specific cookie, if present then returns cookie value otherwise it returns null. Below is the existing code</p>
  
  <pre class="lang-java prettyprint-override"><code>final Cookie[] cookies = request.getCookies();
  if (cookies != null) {
      for (int i = 0; i &lt; cookies.length; i++) {
          final Cookie cookie = cookies[i];
          if ("random cookie".equals(cookie.getName())) {
              return cookie.getValue();
          }
      }
      return null;
  }
  </code></pre>
  
  <p>I refactored it to something like this</p>
  
  <pre class="lang-java prettyprint-override"><code>final Cookie[] cookies = request.getCookies();
  if (cookies != null) {
      Optional&lt;Cookie&gt; cookie = Arrays.stream(cookies).
              filter(e -&gt; "random cookie".equals(e.getName())).
              findAny();
      return cookie.isPresent() ? cookie.get().getValue() : null;
  }
  return null;
  </code></pre>
  
  <p>Can the above be refactored in a better way? </p>