mbs
mbs

Reputation: 421

Search in Hashmap and return value

I'm trying to search in a hashmap in a grails application that looks like this

 [[name: asset1, ips:[10.1.1.1, 10.1.2.3]], [name: asset2, ips:[192.168.1.1, 192.168.1.2, 192.168.1.3]], [name: asset3, [ips: 1.2.3.4, 2.3.4.5, 3.4.5.6]]]

Now I want to search for an ip address (e.g. 10.1.1.1) and want to return the name of the corresponding asset (e.g. asset1).

I have no idea how to do this in grails.

I have tried with findAll(), but have no idea if this is the right direction.

 asset = assetList.findAll{it.value == ipAddress}.each {name << it.key}

Upvotes: 0

Views: 38

Answers (1)

Igor Artamonov
Igor Artamonov

Reputation: 35961

try:

asset = assetList.findAll{ ipAddress in it.ips}.name

Upvotes: 2

Related Questions