Groovy not in collection

前端 未结 8 1162
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:25

    Regarding one of the original answers:

    if (!['a', 'b', 'c'].contains('b'))
    

    Somebody mentioned that it is easy to miss the ! because it's far from the method call. This can be solved by assigning the boolean result to a variable and just negate the variable.

    def present = ['a', 'b', 'c'].contains('b')
    if (!present) {
        // do stuff
    }
    

提交回复
热议问题