How to retrieve a key from the Map<String,List> based on its value (an element of a list)? The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceHow to use a variable for the key part of a mapSerialized JSON with sorted keys, using JacksonMap with Key as String and Value as List in GroovyRemoving a Given Key from a Groovy MapLoop through a group of indexes in groovy until value is foundConvert the groovy map's key value pair to JSON array elementsHow to convert a Groovy map to key=“value” string?Apache NiFi ExecuteScript: Groovy script to replace Json values via a mapping fileHow to retrieve value against key from mapHow to sum map values based on two keys?
Did God make two great lights or did He make the great light two?
He got a vote 80% that of Emmanuel Macron’s
What's the point in a preamp?
Did the UK government pay "millions and millions of dollars" to try to snag Julian Assange?
How to politely respond to generic emails requesting a PhD/job in my lab? Without wasting too much time
Windows 10: How to Lock (not sleep) laptop on lid close?
I could not break this equation. Please help me
How are presidential pardons supposed to be used?
Is there a writing software that you can sort scenes like slides in PowerPoint?
Why can't wing-mounted spoilers be used to steepen approaches?
What aspect of planet Earth must be changed to prevent the industrial revolution?
Why is the object placed in the middle of the sentence here?
Python - Fishing Simulator
What are these Gizmos at Izaña Atmospheric Research Center in Spain?
Is this wall load bearing? Blueprints and photos attached
Can a novice safely splice in wire to lengthen 5V charging cable?
Typeface like Times New Roman but with "tied" percent sign
Single author papers against my advisor's will?
How to copy the contents of all files with a certain name into a new file?
Segmentation fault output is suppressed when piping stdin into a function. Why?
When did F become S? Why?
Why can't devices on different VLANs, but on the same subnet, communicate?
Change bounding box of math glyphs in LuaTeX
How is simplicity better than precision and clarity in prose?
How to retrieve a key from the Map> based on its value (an element of a list)?
The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceHow to use a variable for the key part of a mapSerialized JSON with sorted keys, using JacksonMap with Key as String and Value as List in GroovyRemoving a Given Key from a Groovy MapLoop through a group of indexes in groovy until value is foundConvert the groovy map's key value pair to JSON array elementsHow to convert a Groovy map to key=“value” string?Apache NiFi ExecuteScript: Groovy script to replace Json values via a mapping fileHow to retrieve value against key from mapHow to sum map values based on two keys?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a map looking like this:
def myMap = [
'Key1': ['nameOfApp', 'nameOfApp2', 'nameOfApp3'],
'Key5': ['nameOfApp4'],
'Key2': ['nameOfApp5'],
'Key3': ['xxx', 'yyy'],
'Key4': ['aaa', 'vvv']
]
How can I find the KEY by providing a VALUE (which is a simple String value) ? I've tried lots of things but I'm blocked with the fact that values are lists...
As an example, if I provide nameOfApp2, I expect to get the key Key1. What's the easiest way to achieve it with Groovy?
groovy
add a comment |
I have a map looking like this:
def myMap = [
'Key1': ['nameOfApp', 'nameOfApp2', 'nameOfApp3'],
'Key5': ['nameOfApp4'],
'Key2': ['nameOfApp5'],
'Key3': ['xxx', 'yyy'],
'Key4': ['aaa', 'vvv']
]
How can I find the KEY by providing a VALUE (which is a simple String value) ? I've tried lots of things but I'm blocked with the fact that values are lists...
As an example, if I provide nameOfApp2, I expect to get the key Key1. What's the easiest way to achieve it with Groovy?
groovy
Boggles the mind why anyone would design a map and look up something by “value” why don’t you invert the map?
– Strelok
Mar 8 at 13:55
The fact is that I can't update this part of the code because it's not on my scope. I'm using this map via Jenkins External Library.
– Tony Lecointre
Mar 8 at 14:11
add a comment |
I have a map looking like this:
def myMap = [
'Key1': ['nameOfApp', 'nameOfApp2', 'nameOfApp3'],
'Key5': ['nameOfApp4'],
'Key2': ['nameOfApp5'],
'Key3': ['xxx', 'yyy'],
'Key4': ['aaa', 'vvv']
]
How can I find the KEY by providing a VALUE (which is a simple String value) ? I've tried lots of things but I'm blocked with the fact that values are lists...
As an example, if I provide nameOfApp2, I expect to get the key Key1. What's the easiest way to achieve it with Groovy?
groovy
I have a map looking like this:
def myMap = [
'Key1': ['nameOfApp', 'nameOfApp2', 'nameOfApp3'],
'Key5': ['nameOfApp4'],
'Key2': ['nameOfApp5'],
'Key3': ['xxx', 'yyy'],
'Key4': ['aaa', 'vvv']
]
How can I find the KEY by providing a VALUE (which is a simple String value) ? I've tried lots of things but I'm blocked with the fact that values are lists...
As an example, if I provide nameOfApp2, I expect to get the key Key1. What's the easiest way to achieve it with Groovy?
groovy
groovy
edited Mar 8 at 13:21
Szymon Stepniak
18.9k83566
18.9k83566
asked Mar 8 at 13:10
Tony LecointreTony Lecointre
133
133
Boggles the mind why anyone would design a map and look up something by “value” why don’t you invert the map?
– Strelok
Mar 8 at 13:55
The fact is that I can't update this part of the code because it's not on my scope. I'm using this map via Jenkins External Library.
– Tony Lecointre
Mar 8 at 14:11
add a comment |
Boggles the mind why anyone would design a map and look up something by “value” why don’t you invert the map?
– Strelok
Mar 8 at 13:55
The fact is that I can't update this part of the code because it's not on my scope. I'm using this map via Jenkins External Library.
– Tony Lecointre
Mar 8 at 14:11
Boggles the mind why anyone would design a map and look up something by “value” why don’t you invert the map?
– Strelok
Mar 8 at 13:55
Boggles the mind why anyone would design a map and look up something by “value” why don’t you invert the map?
– Strelok
Mar 8 at 13:55
The fact is that I can't update this part of the code because it's not on my scope. I'm using this map via Jenkins External Library.
– Tony Lecointre
Mar 8 at 14:11
The fact is that I can't update this part of the code because it's not on my scope. I'm using this map via Jenkins External Library.
– Tony Lecointre
Mar 8 at 14:11
add a comment |
1 Answer
1
active
oldest
votes
You can use find method with a predicate like expectedValue in it.value. Check the following example:
def myMap = [
'Key1': ['nameOfApp', 'nameOfApp2', 'nameOfApp3'],
'Key5': ['nameOfApp4'],
'Key2': ['nameOfApp5'],
'Key3': ['xxx', 'yyy'],
'Key4': ['aaa', 'vvv']
]
def expectedValue = 'nameOfApp2'
def key = myMap.find expectedValue in it.value ?.key
assert key == 'Key1'
The variable it inside the predicate closure represents each Map.Entry<String. List<String>> from the input map, so you can check if the expected value is a part of it.value list. And the last part uses ?.key null safe operator to get the value of the key, just in case expected value is not present in any list.
Thanks a lot, it works well. I was really close but my knowledges about Groovy are low. Thanks again :)
– Tony Lecointre
Mar 8 at 13:18
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55063924%2fhow-to-retrieve-a-key-from-the-mapstring-liststring-based-on-its-value-an-e%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use find method with a predicate like expectedValue in it.value. Check the following example:
def myMap = [
'Key1': ['nameOfApp', 'nameOfApp2', 'nameOfApp3'],
'Key5': ['nameOfApp4'],
'Key2': ['nameOfApp5'],
'Key3': ['xxx', 'yyy'],
'Key4': ['aaa', 'vvv']
]
def expectedValue = 'nameOfApp2'
def key = myMap.find expectedValue in it.value ?.key
assert key == 'Key1'
The variable it inside the predicate closure represents each Map.Entry<String. List<String>> from the input map, so you can check if the expected value is a part of it.value list. And the last part uses ?.key null safe operator to get the value of the key, just in case expected value is not present in any list.
Thanks a lot, it works well. I was really close but my knowledges about Groovy are low. Thanks again :)
– Tony Lecointre
Mar 8 at 13:18
add a comment |
You can use find method with a predicate like expectedValue in it.value. Check the following example:
def myMap = [
'Key1': ['nameOfApp', 'nameOfApp2', 'nameOfApp3'],
'Key5': ['nameOfApp4'],
'Key2': ['nameOfApp5'],
'Key3': ['xxx', 'yyy'],
'Key4': ['aaa', 'vvv']
]
def expectedValue = 'nameOfApp2'
def key = myMap.find expectedValue in it.value ?.key
assert key == 'Key1'
The variable it inside the predicate closure represents each Map.Entry<String. List<String>> from the input map, so you can check if the expected value is a part of it.value list. And the last part uses ?.key null safe operator to get the value of the key, just in case expected value is not present in any list.
Thanks a lot, it works well. I was really close but my knowledges about Groovy are low. Thanks again :)
– Tony Lecointre
Mar 8 at 13:18
add a comment |
You can use find method with a predicate like expectedValue in it.value. Check the following example:
def myMap = [
'Key1': ['nameOfApp', 'nameOfApp2', 'nameOfApp3'],
'Key5': ['nameOfApp4'],
'Key2': ['nameOfApp5'],
'Key3': ['xxx', 'yyy'],
'Key4': ['aaa', 'vvv']
]
def expectedValue = 'nameOfApp2'
def key = myMap.find expectedValue in it.value ?.key
assert key == 'Key1'
The variable it inside the predicate closure represents each Map.Entry<String. List<String>> from the input map, so you can check if the expected value is a part of it.value list. And the last part uses ?.key null safe operator to get the value of the key, just in case expected value is not present in any list.
You can use find method with a predicate like expectedValue in it.value. Check the following example:
def myMap = [
'Key1': ['nameOfApp', 'nameOfApp2', 'nameOfApp3'],
'Key5': ['nameOfApp4'],
'Key2': ['nameOfApp5'],
'Key3': ['xxx', 'yyy'],
'Key4': ['aaa', 'vvv']
]
def expectedValue = 'nameOfApp2'
def key = myMap.find expectedValue in it.value ?.key
assert key == 'Key1'
The variable it inside the predicate closure represents each Map.Entry<String. List<String>> from the input map, so you can check if the expected value is a part of it.value list. And the last part uses ?.key null safe operator to get the value of the key, just in case expected value is not present in any list.
edited Mar 8 at 13:52
answered Mar 8 at 13:14
Szymon StepniakSzymon Stepniak
18.9k83566
18.9k83566
Thanks a lot, it works well. I was really close but my knowledges about Groovy are low. Thanks again :)
– Tony Lecointre
Mar 8 at 13:18
add a comment |
Thanks a lot, it works well. I was really close but my knowledges about Groovy are low. Thanks again :)
– Tony Lecointre
Mar 8 at 13:18
Thanks a lot, it works well. I was really close but my knowledges about Groovy are low. Thanks again :)
– Tony Lecointre
Mar 8 at 13:18
Thanks a lot, it works well. I was really close but my knowledges about Groovy are low. Thanks again :)
– Tony Lecointre
Mar 8 at 13:18
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55063924%2fhow-to-retrieve-a-key-from-the-mapstring-liststring-based-on-its-value-an-e%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Boggles the mind why anyone would design a map and look up something by “value” why don’t you invert the map?
– Strelok
Mar 8 at 13:55
The fact is that I can't update this part of the code because it's not on my scope. I'm using this map via Jenkins External Library.
– Tony Lecointre
Mar 8 at 14:11