Is there a way to grab the key of one map and replace the value of the other with its value?
def wild = [animal1:"pet3", animal2:"dog", animal3:"pig"]
def pet = [pet1:"hamster", pet2:"fish", pet3:"cat"]
if(pet.containsKey(wild.animal1)) {
//replace wild.animal1 with the value contained in pet3 for example
//so wild.animal1 would equal "cat"
} else {
//dont change value
}
So basically I'm wondering if I am able to find a key based on a value in the map wild and replacing the value with the value of the key in the map pet.
Is there a simple way of going about this?
if(pet.containsKey(wild.animal1))
{
wild.animal1 = pet[wild.animal1];
}
Related
Compare two text files using groovy in Jenkins slave machine. Sample text files are as follows.
Sample1.txt:
team_a, added
team_b, removed
team_c, added
Sample2.txt:
team_d, added
team_e, added
team_c, removed
Need to identify the change and should give the output as team_c has been removed.
Asking this question as being newbie to groovy.
You haven't posted your code, but say you are a newbie, so I'll talk you through an example:
// For simplicity, I won't use files, but a "heredoc"
Sample1txt = '''
team_a, added
team_b, removed
team_c, added
'''
Sample2txt ='''
team_d, added
team_e, added
team_c, removed
'''
//get the data in a map each
Map a = [:]
Sample1txt.splitEachLine(",") { line -> a[line[0]] = line[1]}
Map b = [:]
Sample2txt.splitEachLine(",") { line -> b[line[0]] = line[1]}
//for each key value pair in the second map...
b.each {k, v ->
//if the key exists (you didn't say what you want to do with new keys)
if (a.containsKey(k)) {
//and if the key's value is not equal to the same key's value in the first map
if (!b[k].equals(a[k])) {
//print it
println "${k} : ${v}"
}
}
}
This is not a robust solution, but it will point you in the right direction.
I am trying to get the first element of the object scan. In my case, the first element's key changes. So I cannot call it with the key. Here is the AQL query I'm using, which is not working.
`FOR d in collection RETURN DISTINCT Object.keys(d.out.scan)[0]`
Object structure:
{
"out": {
"scan":{
"someKeyThatChanges":"someValue"
}
}
}
Is there a way to fetch the first key of scan?
Thank you
The relevant AQL functions for this issue are documented at
https://docs.arangodb.com/3.3/AQL/Functions/Document.html
In brief, if the object has only one user-defined key, then you will
be able to use VALUES(_, true) directly.
Otherwise, you could use ATTRIBUTES() to get an array of the object's
attributes. You may want to filter it to avoid keys with names that start with "_". Once you've selected a key, remember:
Attributes can also be accessed using the [] accessor
... the square brackets allow for expressions:
... u[attr1][0][attr2][ CONCAT("fir", "st") ]
Demo
LET x = {
"out": {
"scan":{
"someKeyThatChanges":"someValue"
}
}
}
LET y = x.out.scan
LET z = y[ ATTRIBUTES(y)[0] ]
RETURN z
To fetch just the name of the first key of out.scan, the following will work:
FOR d IN collection
RETURN ATTRIBUTES(d.out.scan)[0]
For returning the mapped value for that key, please refer to the other answer given.
Following the comments on Mongoose: how to define a combination of fields to be unique?
First let's get the array of data sorted by all values which supposed to be unique.
Assuming we're talking about strings (as in this question), we can combine them to create one long string that is supposed to be unique.
Being sorted, if there are duplicate values they'll show up right after the other, so let's look for results that repeat themselves:
var previousName;
Person.find().sort('firstName lastName').exec().each(function (person) {
var name = person.firstName + person.lastName;
if (name == previousName) {
console.log(name);
person.remove();
}
previousName = name;
})
I am wondering about how to search in J2ME. I have been searching in the internet, so many result show to me, and I see in Java2s.com I got a result use RecordFilter and matches method for search in record store.
But my problem is, when I need to pass 2 or more parameters into it. How can result matches with these parameter?
And how to sort descending or ascending like bubble sort?
Concatenate your searches into a single String variable. Separate each of them with ; for example. In the code of the matches method explode the String to get each search criteria.
To make the filter in effect create an instance of SearchFilter and call the matches method with the concantenated String as its param.
For the sort implement the RecordComparator interface ; implement the compare method to build your sort criteria. Make a google search about j2me+recordcomparator to see examples about how to make sorts.
EDIT :
In the code of the matches method explode the String param obtained from the byte[] param. Treat each String exploded to make the criteria.
As I understand you want to pass two string as a search criteria when you wrote :
SearchFilter search = new SearchFilter(txtSearch.getString(), strType);
So in the constructor there should be two params !!!
When you want to make the matching then call
if searchFilter.matches((search1+";"+sType).getBytes())
Then explode the candidate param into two String when you code the matches method.
When I save my Data in RMS I save it as a String[] like I want to save Name, Age,Salary,EmpID for each employee I save it create an array and convert it to bytes and save it in RMS. When i retrieve it i do the reverse process. Now if i want to get employee with names starting with A and with salary 10000 i use the following filter
class UtilFilter implements RecordFilter{
public UtilFilter(String str_searchText,String str_searchText1)
{
this.str_searchText = str_searchText.toLowerCase();
this.str_searchText1 = str_searchText1.toLowerCase();
}
public boolean matches(byte[] bt_byteData)
{
String str_str = "";
String str_str1 = "";
//here goes code how u get back ur String[] from RMS say u get it in Data
str_str = Data[0].trim();
str_str1 = gd_cd.Data[2].trim();
if(str_searchText != null && str_searchText1 != null && str_str.equals(str_searchText) && str_str1.equals(str_searchText1 ))
{
return true;
}
else
{
return false;
}
}
}
This way i can filter any no of parameters.Hope tht helps! :)
I have a list which contains more than 75 thousand object. To search item from list currently I am using following code.
from nd in this.m_ListNodes
where
nd.Label == SearchValue.ToString()
select
nd;
Is this code is efficient?
How often do you need to search the same list? If you're only searching once, you might as well do a straight linear search - although you can make your current code slightly more efficient by calling SearchValue.ToString() once before the query.
If you're going to perform this search on the same list multiple times, you should either build a Lookup or a Dictionary:
var lookup = m_ListNodes.ToLookup(nd => nd.Label);
or
var dictionary = m_ListNodes.ToDictionary(nd => nd.Label);
Use a dictionary if there's exactly one entry per label; use a lookup if there may be multiple matches.
To use these, for a lookup:
var results = lookup[SearchValue.ToString()];
// results will now contain all the matching results
or for a dictionary:
WhateverType result;
if (dictionary.TryGetValue(SearchValue.ToString(), out result))
{
// Result found, stored in the result variable
}
else
{
// No such item
}
No. It would be better if you used a Dictionary or a HashSet with the label as the key. In your case a Dictionary is the better choice:
var dictionary = new Dictionary<string, IList<Item>>();
// somehow fill dictionary
IList<Item> result;
if(!dictionary.TryGetValue(SearchValue.ToString(), out result)
{
// if you need an empty list
// instead of null, if the SearchValue isn't in the dictionary
result = new List<Item>();
}
// result contains all items that have the key SearchValue