i want to update an item (in java). item may contain map m. that map may contain a string set s.
i need to add new elements to m.s creating m and/or s if they don't exist. is it possible? can this be done using updateExpression?
Related
I have to insert data inside Set in Java code.
To identify duplicates based on id in entity, I have overridden equals and hashcode method.
Now, the values with duplicate id are rejected when trying to add inside Set.
Present behavior - Set rejects new data with id already existing in Set
Expected: How to make Set replace the new duplicate values with the same id instead of rejecting? [similar to Map which replaces duplicate key ]
Thanks
If I run the code below, the value coming from getUserName() gets appended to the list. But if I run it again, same value gets inserted again. My question is, how to make sure the value is appended only once(Unique)?
My current code
Operation operation = ListOperation.append("names", Value.get(usr.getUserName()));
Record record = client.operate( policy, key, operation );
Example: Consider the "name" bin to be a list of strings.
'LIST["T1", "T2"]'
If I add T2 again, the list should remain the same.
ListOperation.append() can take a ListPolicy, which takes flags. For write operations such as this, the ListWriteFlags have the fields ADD_UNIQUE and NO_FAIL which you should combine if you want a value to only be added if it's a new unique list element.
See https://www.slideshare.net/RonenBotzer/asug-tlvmeetup2talk
i'm assuming that ListOperation is backed by a List since the code isn't available. Assuming it is, Lists maintain order while allowing null values. Sets on the other hand only allow unique, non-null, values so this would be a perfect solution for you.
Set<String> values = new HashSet<>();
values.add("T1");
values.add("T1"); // already contains value
Alternative
If you need to maintain the order of your Collection but you want unique values, another option would be to check on add if the value already exists OR using Stream#distinct.
List<String> values = ListOperation.values.stream().distinct().collect(Collectors.toList());
www.abcd.com/user/getuserstats.htm?userId=123123
In this api, the userId gets set to a field named userId in the Action class mapped to this action.
Now,for this
www.abcd.com/user/getuserstats.htm?listOfUsers=123123,456456,789789,42568,58963
I need to know how can we map this list of userIds in an ArrayList defined in the corresponding Action class so that it gets mapped as an ArrayList not as a String.
Note : I don't want to get a a string of userIds and convert it to ArrayList later.I want that the list of the userIds be automatically mapped into a list or an ArrayList. I am sure there must a way to achive that.
basically , i found that it can be achieved like this :
www.abcd.com/user/getuserstats.htm?userId=123123&userId=4578&userId=567&userid=987
these params will go into an arraylist already declared in the action class.
The values can be submitted in CSV format. Struts2 has a built-in type converter which is able to populate a property that is a collection such as list.
A workaround by using multiple parameters with the same name is possible but it works without using Struts2 type conversion. The last is one of the powerful feature provided by the framework and it is smart less not to use it.
More detailed explanation of this feature is here.
The values should be in the CSV format like in this answer.
That will also give you an idea about type of property which you
should bind to the hidden field. For example you can use List
or Integer[] for the property that set the values 25, 27, 28.
Struts2 has a built-in converter that converts such values to the list
or array automatically.
I have an RDF Ontology with a functional property hasTrendValue which relates instances of a class with integer values. I want to change these values programmatically using Jena. I tried the following code:
Property hasTrend = ontModel.getDatatypeProperty(preFix+"hasTrendValue");
Individual regionQualifier = ontModel.getIndividual(activityName);
ontModel.addLiteral(regionQualifier,hasTrend,34);
PrintStream p = new PrintStream(ontoPath);
ontModel.write(p,null);
p.close();
This code executes correctly but, it does not update the already hasTrendValue value in the RDF; instead it adds a new hasTrendValue to the RDF ontology even though it declared as a functional property. What is the better way of doing this?
RDF does not have the concept of "change", only "add" and "remove". To change a value, you need to remove the old one and add the new one.
Declaring it as a functional property does not change this. Jena does not check the ontology on every operation. In fact, a functional property says that the object identifies one thing - it may be written in many ways. 001 and 1 are the same value. There may be multiple triples, it's not automatically wrong.
I have a MultiValueMap like
{3=[c-2, c-2], 2=[b-1, b-1], 1=[a-1, a-2, a-3]}
At one point I have to update a single value of a specific key
for example I have to update the key 2 like
2=[u-1,u-2]
how can i do this?
I've never used that library - but I would expect these two examples to do what you need:
multiMap.getCollection(2).clear();
multiMap.putAll(2, Arrays.asList("u-1", "u-2"));
Or
Collection c = multiMap.getCollection(2);
c.clear();
Collections.addAll(c, "u-1", "u-2");
The safest way is to call getCollection() to retrieve the current mapping, remove(key) to clear that mapping, iterate the retrieved collection to re-insert values that you want to keep, and/or add the new values.
If you know the type of collection used for a mapping (because you've called the constructor that takes collectionFactory), you could get the collection and update it directly.