检查列表包含元素或仅包含一个元素

时间:2017-06-27 13:53:19

标签: groovy

是否存在检查列表包含两个元素的忍者技巧?我正在考虑像任何一样

基本上我希望重写下面的代码:

List<String> elements = ["first", "fourth"]
List<String> longList = ["first", "second", "third", "fourth"]
boolean haveAll = elements ? true : false

elements.each { String element ->
    haveAll &= longList.any {element==it}
}

assert haveAll == true

longList = ["first", "second", "third"]
elements.each { String element ->
    haveAll &= longList.any {element==it}
}

assert haveAll == false

2 个答案:

答案 0 :(得分:2)

您可以使用everyany

def elements = ["first", "fourth"]
def longList = ["first", "second", "third", "fourth"]

assert elements.every { it in longList }
assert elements.any { it in longList }

答案 1 :(得分:1)

我怀疑这是否属于 ninja trick ,但它似乎可以胜任

@Component({
  selector: 'app-product-card',
  moduleId: module.id,
  templateUrl: 'product-card.component.html'
})

export class ProductCardComponent {
  @Input() products: Product[];
}