Groovy not in collection

前端 未结 8 1289
Happy的楠姐
Happy的楠姐 2021-02-04 23:50

The groovy way to see if something is in a list is to use \"in\"

   if(\'b\' in [\'a\',\'b\',\'c\'])

However how do you nicely see if something

8条回答
  •  没有蜡笔的小新
    2021-02-05 00:04

    More readable, I'm not sure:

    assert ['a','b','c'].any{it == 'b'}
    assert ['a','b','c'].every{it != 'g'}
    

    For your example:

    if (['a','b','c'].every{it != 'g'})
    

    A few months ago, I suggested a new operator overloading ! (not operator). Now, maybe you can use any odd number of exclamations ;)

    if(!!!('g' in ['a','b','c']))
    

提交回复
热议问题