HashMap value (object) incorrectly overridden with incorrect value - hashmap

I am iterating over a hashmap that is made of string keys and has objects as values and I am setting the attributes of the objects. The value of my hashmap is an object which has other objects as attributes. The problem is that once I set the value for the first object with key "1-1", in the second iteration when I want to set the value for my second key"2-1", the value corresponding to the first entry which was assigned right before ends up overridden with the value of the second entry, in other words, the attribute value of the first entry ends up overridden with the attribute value corresponding to the second entry. More specifically for key "1-1", in the first iteration of my hashmap, when I print methodtraceHashMap2.get("1-1").Method.Owner.getDeveloperGold(), I get the value "T", then as soon as I move to the second key with value "2-1", I end up with a different value when I reprint methodtraceHashMap2.get("1-1").Method.Owner.getDeveloperGold(), namely I end up with "N", while it should be "T" in reality, the "N" I am getting actually coresponds to the value of methodtraceHashMap2.get("2-1").Method.Owner.getDeveloperGold() which was set for the second entry of my hashmap which has the key "2-1"
for(MethodTrace MethodTrace: methodtraceHashMap2.values()) {
String reqClass=MethodTrace.Requirement.ID+"-"+MethodTrace.Method.Owner.ID;
MethodTrace.Method.Owner.DeveloperGold=classTraceHashMap.get(reqClass).DeveloperGold;
System.out.println("yes");
}
System.out.println(methodtraceHashMap2.get("1-1").Method.Owner.getDeveloperGold());
Here is the snippet that populates the map but the problem arises in the previous snippet when I populate the attribute MethodTrace.Method.Owner.DeveloperGold of the MethodTrace, I do not believe that the problem is linked to the snippet below
ResultSet myresults = st.executeQuery("SELECT traces.* from traces");
while (myresults.next()) {
MethodTrace MethodTrace = new MethodTrace();
Method method= new Method();
Requirement requirement= new Requirement();
requirement=RequirementHashMap.get(myresults.getString("requirementid"));
method = MethodHashMap.get(myresults.getString("methodid"));
MethodTrace.setMethod(method);
MethodTrace.setRequirement(requirement);
//checking whether the method is present in the superclasses
MethodTrace.setGold(myresults.getString("goldfinal"));
String reqMethod=MethodTrace.Requirement.ID+"-"+MethodTrace.Method.ID;
String reqClass=MethodTrace.Requirement.ID+"-"+MethodTrace.Method.Owner.ID;
MethodTrace.Method.Owner.DeveloperGold=classTraceHashMap.get(reqClass).DeveloperGold;
System.out.println(reqMethod+"-");
System.out.println(MethodTrace.Method.Owner.DeveloperGold);
methodtraceHashMap2.putIfAbsent(reqMethod, MethodTrace);
System.out.println("WE ARW IN THE LOOP "+methodtraceHashMap2.get("1-1"));
methodtraceHashMap2.putIfAbsent(reqMethod+"_"+System.currentTimeMillis(), MethodTrace);
System.out.println("WE ARW IN THE LOOP "+methodtraceHashMap2.get("1-1"));
}

Related

How to extract data as a List from Set with dynamic keys in terraform

I have below set in terraform (which is extracted from data module)
{
key1 {
id = "5"
name = "A"
},
key2 {
id = "6"
name = "A"
}
}
keys are dynamic, and it will be n number of them, any value.
How can I get the below result? Please notice it's List of strings
[
"5",
"6"
]
I tried below, but it says Unsupported attribute
output "email_channels_keys" {
value = var.emails.*.id
}
We can use a for lambda expression to iterate on the map(object)) and extract the value for the id key in the object within each map:
output "email_channels_keys" {
value = [ for key, value in data.data_name.block_name.attribute : value.id ]
}
We use a list constructor to instantiate the type as a list. We then iterate over the map and store the string key in the temporary lambda scope variable key, and the object value in the temporary lambda scope variable value. We then access the value of the id key within the object with the normal usage of .id (["id"] is also valid syntax, but conventionally map values are accessed with ["<key>"] and object values with .<key> syntax). The returned value is assigned to email_channels_keys in your outputs.
Note that for your specific use case, you will need to update the data namespace for your specific data that you reference at the beginning of the question, and you may want to update key and value variables for more specific names.

How do you query a CouchDB view that emits complex keys?

Given a CouchDB view that emits keys of the following format:
[ "part1", { "property": "part2" } ]
How can you find all documents with a given value for part1?
If part2 was a simple string rather than an object startkey=["part1"]&endkey=["part1",{}] would work. The CouchDB docs state the following:
The query startkey=["foo"]&endkey=["foo",{}] will match most array keys with "foo" in the first element, such as ["foo","bar"] and ["foo",["bar","baz"]]. However it will not match ["foo",{"an":"object"}]
Unfortunately, the documentation doesn't offer any suggestion on how to deal with such keys.
The second element of your endkey value needs to be an object that collates after any possible value of the second element of your key. Objects are compared by property-by-property (for example, {"a":1} < {"a":2} < {"b":1}) so the best way to do this is to set the first property name in your endkey to a very large value:
startkey=["part1"]&endkey=["part1", { "\uFFF0": false }]
The property name of \uFFF0 should collate after any other property names in the second key element, and even works when the second element is an empty object or has more than one property.

How can i get value from geb content?

I have some geb content like this
buttonName(wait: true){$("a.btn_primary")}
I need get value from {} i.e. i need string $("a.btn_primary")
For example def value = "$("a.btn_primary")"
If your buttonName is correct, then try this:
def value = buttonName.text()
Cheers!
Try
def value = buttonName.value()
if .text() does not work.
From The Book of Geb,
"The value of input, select and textarea elements can be retrieved and set with the value method. Calling value() with no arguments will return the String value of the first element in the Navigator.
Calling value(value) will set the current value of all elements in the Navigator. The argument can be of any type and will be coerced to a String if necessary. The exceptions are that when setting a checkbox value the method expects a boolean (or, an existing checkbox value) and when setting a multiple select the method expects an array or Collection of values."
Hope it works out!

Groovy - Collection only returns a single value

I've got a small code snippet that loops through a node and grabs all its properties.
I can get this to work if I set one variable to grab the properties values (except it has a weird [] surrounding it). But I don't want redundant code so I'm trying to set multiple properties inside the loop, except all that returns is a single value, it's not looping around all the nodes.
WORKING
String selectNodeLabel = null
selectNodeLabel = JcrUtils.getChildNodes("links").collect{
it.getProperty("label").getString()
}
SINGLE VALUE
String selectNodeLabel = null
String selectNodeMeta = null
String selectNodeFooter= null
String topicNode = null
topicNode = JcrUtils.getChildNodes("links").collect{
selectNodeLabel = it.getProperty("label").getString()
selectNodeMeta = it.getProperty("meta").getString()
selectNodeFooter = it.getProperty("footer").getString()
}
Thanks for any help!
Try:
def nodeList = JcrUtils.getChildNodes("links").collect{
[ selectNodeLabel : it.getProperty("label").getString()
selectNodeMeta : it.getProperty("meta").getString()
selectNodeFooter : it.getProperty("footer").getString() ]
}
Then, nodeList will be a list of Maps, so you could do:
println nodeList*.selectNodeLabel
To print all the selectNodeLabel values for example.
To explain the problems with your code... Collect creates a list of the elements returned by the closure. What your SINGLE VALUE code is doing is overwriting the values in the selectNode... variables, and then setting topicNode to the value returned from the closure for each element in JcrUtils.getChildNodes("links").
For this case, topicNode will contain a List of it.getProperty("footer").getString() (as it is the last line in the Closure

dictionary of class instances, how to assign new value?

I have a dictionary that contains instances of a class. Somehow i can not find a way to change a value (assign new class instance) to dictionary.
For example
Dim t as new Product
'initialize t
if dictionaryP.Exists(keyValue) then
'in the next line i get an error "Object doesn't support this property or method"
dictionaryP.Item(keyValue)=t
else
'no problem with this line...
dictionaryP.Add keyValue, t
end if
Couldn't find any information about using dictionaries in VBA with values that are objects, not just plain strings or integers.
How can i change dictionary value for dictionaries that stores objects (class instances), as it seems that i can not do it using
dictionary.Item(key) = <new Object value> ' as i thought it sould be, from this source
http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/A_3391-Using-the-Dictionary-Class-in-VBA.html
What am i missing? How can i change dictionary values, if values are objects (not plain values)?
Objects must be assigned using Set. The property .Item is the default property and can therefore be dropped (but does not harm). Try
Set dictionaryP (keyValue) = t

Resources