Search code examples
javalambdajava-8java-6

How can I write this simple Java 8 line of code into Java 7


I'm trying to implement a CSRF token, but all the info is programmed with Java 8+ version, so what I need is some help rewriting this line in Java 6/7:

tokenCookie = Arrays.stream(httpReq.getCookies()).filter(c -> c.getName().equals(csrfCookieExpectedName)).findFirst().orElse(null);

Actually from that line I'm only getting the error in:

c -> c.getName().equals(csrfCookieExpectedName)


Solution

  • In addition to @GIO's answer, you could try for-each loop like this:

    public Cookie getExpectedCookieName(Cookie[] cookies) {
        for (Cookie c : cookies) {
            if (c.getName().equals(csrfCookiesExpectedName)) {
                return c;
            }
        }
        return null;
    }
    

    and call it like this : tokenCookie = getExpectedCookieName(httpReq.getCookies());