Groovy web console

subscribe to the feed Subscribe
to this
Reverse map (via #groovywebconsole)
tweet this snippet Tweet

Reverse map

Published 11 months ago by Anonymous
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def keyForValue(map, value) {     
  map.find { it.value == value }?.key 

def map = [a: 1, b: 2, c: 3] 
assert keyForValue(map, 2) == 'b' 
assert keyForValue(map, 42) == null
println 'Everything went fine'