Java 数组的特定元素匹配多个条件并返回布尔值

Java 数组的特定元素匹配多个条件并返回布尔值,java,performance,java-stream,Java,Performance,Java Stream,我希望以最优化和最简单的方式在Java 8中生成此代码: (Authentication auth, HttpServletRequest request, ....) ... Cookie cookies[] = request.getCookies(); if (cookies != null) { for (Cookie cookie : cookies) { if (cookie.getName().equals("_id")) {

我希望以最优化和最简单的方式在Java 8中生成此代码:


(Authentication auth, HttpServletRequest request, ....)

...

Cookie cookies[] = request.getCookies();
if (cookies != null) {

    for (Cookie cookie : cookies) {
        if (cookie.getName().equals("_id")) {
            boolean bool_1 = isIdCookieValid(cookie, auth);
        }
        else if (cookie.getName().equals("XSRF-TOKEN")) {
            boolean bool_2 = isXSRFTokenValid(cookie, request);
        }
    }

    if (bool_1 && bool_2) {
        System.out.println("Ok");
    }
    // else if they are not both true do nothing
}

也许使用Stream类?

您可以提前完成任务,前提是您知道:

  • 结果布尔值为false
  • 你已经计算了两个布尔值
  • 因此:


    如果您知道以下情况之一,则可以提前完成任务:

  • 结果布尔值为false
  • 你已经计算了两个布尔值
  • 因此:


    也许有一个更简单的方法:

    • 只需更换您的

    也许有一个更简单的方法:

    • 只需更换您的

    如果你想要最快的代码,似乎不是做它的方式感谢提示,我不知道这个基准如果你想要最快的代码,似乎不是做它的方式感谢提示,我不知道这个基准事实上,这可能比等待结束循环要快谢谢!我等待更多的解决方案(可能更简洁),然后将此线程标记为已解决。实际上,这可能比等待结束循环要快,谢谢!我等待更多的解决方案(可能更简洁),然后将此线程标记为已解决。谢谢!这比我的代码更干净。我将检查这和波希米亚解决方案之间的性能。谢谢!这比我的代码更干净。我将检查这与波希米亚解决方案之间的性能。
    // Use wrapped Booleans to distinguish between true, false and "no value yet"
    Boolean bool_1 = null;
    Boolean bool_2 = null;
    Cookie cookies[] = request.getCookies();
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("_id")) {
                bool_1 = isIdCookieValid(cookie, auth);
                if (!bool_1) {
                    break;
                }
            }
            else if (cookie.getName().equals("XSRF-TOKEN")) {
                bool_2 = isXSRFTokenValid(cookie, request);
                if (!bool_2) {
                    break;
                }
            }
            if (bool_1 != null && bool_2 != null) {
                break;
            }
        }
    
        if (bool_1 != null && bool_1 != null && bool_1 && bool_2) {
            System.out.println("Ok");
        }
        // else if they are not both true do nothing
    }
    
    Cookie cookies[] = request.getCookies();
    if (cookies != null) {
    
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("_id")) {
                boolean bool_1 = isIdCookieValid(cookie, auth);
            }
            else if (cookie.getName().equals("XSRF-TOKEN")) {
                boolean bool_2 = isXSRFTokenValid(cookie, request);
            }
        }
    
        if (bool_1 && bool_2) {
            System.out.println("Ok");
        }
        // else if they are not both true do nothing
    }
    
    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        boolean bool = Arrays.stream(cookies)
                             .filter(cookie -> 
                                (cookie.getName().equals("_id") ? 
                                  isIdCookieValid(cookie, auth) : 
                                  cookie.getName().equals("XSRF-TOKEN") && 
                                    isXSRFTokenValid(cookie, request)))
                             .count() == 2;
    
        System.out.println(bool ? "Ok" : "")
    }