2017-06-27 10 views
0

リストをチェックするための忍者トリックに両方の要素が含まれていますか?リストのチェックには両方の要素が含まれているかどちらか一方のみが含まれています

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 

答えて

1

私は、これは忍者トリックとしての資格を疑うが、仕事

List<String> longList = ["first", "second", "third", "fourth"] 

boolean hasAll = longList.containsAll(["first", "fourth"]) 
boolean hasAny = ["not in long list", "first"].any { 
    it in longList 
} 

// check that it worked 
assert hasAll && hasAny 
を行うようだ:私は基本的に、私は以下のコードを書き換えることにしたい任意の

のようなものを考えています

2

を使用できeveryany

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

assert elements.every { it in longList } 
assert elements.any { it in longList } 
関連する問題