Get values from map by list of keys in groovy - list

I have a list of strings, i would like to get values from a map, using this list of keys.
For example:
ListOfKeys = ["one", "two", "three"]
map =["one":1,"two":2,"three":3,"four":4]
I should get only the first three because they are in the map, the rest are not.
Thanks

ListOfKeys = ["one", "two", "three"]
map =["one":1,"two":2,"three":3,"four":4]
def values = ListOfKeys.collectEntries{ map[it] }

ListOfKeys = ["one", "two", "three"];
map =["one":1,"two":2,"three":3,"four":4];
result = ListOfKeys.collectEntries{[it,map[it]]}
println result

Related

Sort a nested structure in Coldfusion

I am trying to sort a nested struct using StructSort. Below is a simple example of what I am trying to do. The code below does not work and returns the following error "The specified element a does not contain a simple value." Is it possible to sort a nested structure in Coldfusion, and if so how?
<cfscript>
data = {"a":{"name":100},"b":{"name":50},"c":{"name":25},"d":{"name":75}};
dataSorted= StructSort(data, function(a,b) {
return compare(a.name, b.name);
});
writeDump(dataSorted);
</cfscript>
expected output
c
b
d
a
Also made a cffiddle here: https://cffiddle.org/app/e20a782a-be90-4e65-83de-e31eb83fdf4f
Docs: https://docs.lucee.org/reference/objects/struct/sort.html
<cfscript>
data = {
"a": {"name": 100},
"b": {"name": 50},
"c": {"name": 25},
"d": {"name": 75}
};
dataSorted = data.sort("numeric", "asc", "name")
writeDump(dataSorted);
</cfscript>
Result: array ["c", "b", "d", "a"]
This worked for me on lucee 5. something. The last argument in the sort function is the pathToSubElement within a struct. Fo your example it is simply one level deep using the name property.

How to perform a search in array of object flutter?

I am having a Map list with key and values, for example :
map<String, dynamic> my_List = [{"name": "mike", "age": "20"}, {"name":"william","age": "23"}].
I already tried containsValue but I don't want to use it.
The result i need to get is, when i search for m i need to get [{"name": "mike", "age": "20"}, {"name":"william","age": "23"}] , and when i search 3 i need the result as [{"name":"william","age": "23"}].
You could create a Person or User class as julemand101 has suggested but if You have to work with Map try this:
List<Map<String, dynamic>> search(String input){
return my_List.where((e) => e["name"].contains(input) || e["age"].contains(input)).toList();
}

List<Map<String, Object>> to Map<String, String[]>

I have a List of Map List<Map<String, Object>>. I want to move to Map<String, String[]>
Can someone please let me know how to convert?
List<Map<String, Object>> currentList = new ArrayList<Map<String, Object>>();
Map<String, Object> currMap = new HashMap<String, Object>();
currMap.put("A", "ABC");
currMap.put("B", "PQR");
currMap.put("C", "XYZ");
currentList.add(currMap);
currMap.put("A", "123");
currMap.put("B", "456");
currMap.put("C", "789");
currentList.add(currMap);
currMap.put("A", "OOO");
currMap.put("B", "ZZZ");
currentList.add(currMap);
To-be :
"A", ["ABC", "123", "OOO"],
"B", ["PQR", "456", "ZZZ"],
"C", ["XYZ", "789", ""]
One way using streams would be:
currentList.stream()
.flatMap(map -> map.entrySet().stream())
.collect(Collectors.groupingBy(Entry::getKey, Collectors.mapping(Entry::getValue, Collectors.toList())));
This would result in:
{A=[ABC, 123, OOO], B=[PQR, 456, ZZZ], C=[XYZ, 789]}
As a sidenote, the way you are generating currentList in the example does not result in a list of maps as you are probably expecting. As it is, you will end up with 3 references to the same map in the list.

groovy: create a list of values with all strings

I am trying to iterate through a map and create a new map value. The below is the input
def map = [[name: 'hello', email: ['on', 'off'] ], [ name: 'bye', email: ['abc', 'xyz']]]
I want the resulting data to be like:
[hello: ['on', 'off'], bye: ['abc', 'xyz']]
The code I have right now -
result = [:]
map.each { key ->
result[random] = key.email.each {random ->
"$random"
}
}
return result
The above code returns
[hello: [on, off], bye: [abc, xyz]]
As you can see from above, the quotes from on, off and abc, xyz have disappeared, which is causing problems for me when i am trying to do checks on the list value [on, off]
It should not matter. If you see the result in Groovy console, they are still String.
Below should be sufficient:
map.collectEntries {
[ it.name, it.email ]
}
If you still need the single quotes to create a GString instead of a String, then below tweak would be required:
map.collectEntries {
[ it.name, it.email.collect { "'$it'" } ]
}
I personally do not see any reasoning behind doing the later way. BTW, map is not a Map, it is a List, you can rename it to avoid unnecessary confusions.
You could convert it to a json object and then everything will have quotes
This does it. There should/may be a groovier way though.
def listOfMaps = [[name: 'hello', email: ['on', 'off'] ], [ name: 'bye', email: ['abc', 'xyz']]]
def result = [:]
listOfMaps.each { map ->
def list = map.collect { k, v ->
v
}
result[list[0]] = ["'${list[1][0]}'", "'${list[1][1]}'"]
}
println result

Reorder a list based on example list

I would like to know how to reorder a list of lists based on an example list.
Here is an example to clarify my question:
["Foo", "Bar", "Something"]
That is example list, now I want to reorder the list shown below, looking only at the heads of each list inside it:
[["Something", "one", "two", "three"], ["Foo", "four", "five", "six"],
["Bar", "seven", "eight", "nine"]]
to this:
[["Foo", "four", "five", "six"], ["Bar", "seven", "eight", "nine"],
["Something", "one", "two", "three"]]
Thanks
Edit:
I've tried mapping a swap function that swaps two elements, but that won't work because I am dealing here with two lists.
Probably a very brute approach:
Prelude> let a = [["Something", "one", "two", "three"], ["Foo", "four", "five", "six"], ["Bar", "seven", "eight", "nine"]]
Prelude> let b = ["Foo", "Bar", "Something"]
Prelude> concatMap (\y -> filter (\(x:xs) -> x == y) a) b
[["Foo","four","five","six"],["Bar","seven","eight","nine"],["Something","one","two","three"]]
Another solution based on list comprehension can be :
Prelude> let a = [["Something", "one", "two", "three"], ["Foo", "four", "five", "six"], ["Bar", "seven", "eight", "nine"]]
Prelude> let b = ["Foo", "Bar", "Something"]
Prelude> let c = [ y | x<-b, y<-a, x `elem` y ]
[["Foo","four","five","six"],["Bar","seven","eight","nine"],["Something","one","two","three"]]
you can read this chapter for more explanation on how to use list comprehension: Lyah
You want to sort your list according to a custom comparison. So, let's do it! The basic comparison function should look at the index a value appears in the examples list.
import Data.List
cmpIndex :: Eq a => [a] -> [a] -> [a] -> Ordering
cmpIndex example s1 s2 = compare (indexOf s1) (indexOf s2)
where indexOf s = findIndex (head s ==) example
This is a little dangerous -- calling head is always a cause to pause. But let's assume you know something I don't and move on. We'll give names to our inputs to make the test more readable, then fire up ghci:
example = ["Foo", "Bar", "Something"]
list = [["Something", "one", "two", "three"], ["Foo", "four", "five", "six"], ["Bar", "seven", "eight", "nine"]]
*Main> sortBy (cmpIndex example) list
[["Foo","four","five","six"],["Bar","seven","eight","nine"],["Something","one","two","three"]]