Java groovy流比较两个列表并返回单个布尔变量

时间:2019-07-18 03:05:42

标签: java groovy java-stream

我正在尝试比较两个不同列表中元素的属性,如果所有元素都匹配,则返回单个布尔变量。我正试图做到这一点。

def "Test1"() {
    given:
    ...

    when:
    ...

    then: "Compare results"
    IntStream.range(0, 2).allMatch((index) -> packagesMatch(requests.get(index), responses.get(index)));
}

private boolean packagesMatch(Request request, Response response) {
    boolean match = (request.property1 == response.property1)
    match = match && (request.property2 == response.property2)
    match = match && (request.property3 == response.property3)
    match = match && (request.property4 == response.property4)
    return match
}

以上代码在->处出现编译错误:“ Groovyc:意外令牌:->”。将鼠标指针悬停在该行上会给出提示:

<expression> or '=' expected, got '->'

1 个答案:

答案 0 :(得分:0)

Groovy使用其{ }关闭语法。也就是说,看来您真正想要做的是:

then:
(0..2).every { packagesMatch(requests[it], responses[it]) }

甚至:

then:
[requests, responses].transpose().every { request, response -> packagesMatch(request, response) }

(根据您的真实类型,还有一种更好的措辞短语。您的packagesMatch可能是,而且还猜测您的property名称实际上并没有顺序编号:< / p>

return ["property1", "property2"].every { request."$it" == response."$it" }