Finding the key in a map, given the value

前端 未结 5 2658
迷失自我
迷失自我 2021-02-20 16:57

Hi I have a map like this :

[this:0, is:1, a:2, file:3, anotherkey:4, aa:5]

I wish I could find the key\'s given the value

5条回答
  •  一整个雨季
    2021-02-20 17:35

    You could invert the map, like this:

    Map m = [a: '1', b: '2']
    Map mInvert = m.collectEntries { e -> [(e.value): e.key] }
    
    assert mInvert == ['1':'a', '2':'b']
    
    assert mInvert['2'] == 'b'
    assert m['b'] == '2'
    

提交回复
热议问题