Groovy not in collection

前端 未结 8 1242
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:20

    You can add new functions:

    Object.metaClass.notIn = { Object collection ->
        !(delegate in collection)
    }
    
    
    assert "2".notIn(['1', '2q', '3'])
    assert !"2".notIn(['1', '2', '3'])
    
    assert 2.notIn([1, 3])
    assert !2.notIn([1, 2, 3])
    
    assert 2.notIn([1: 'a', 3: 'b'])
    assert !2.notIn([1: 'a', 2: 'b'])
    

提交回复
热议问题