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
Related
You you can see from the class name properties.stringPropertyNames() returns an Collections$UnmodifiableCollection. Unmodifiable means you can't add, insert, remove or change something on this collection
Now that you have explained what the type of properties is java.util.Properties the answer is clear. In Java 8, Properties.stringPropertyNames() returned a set whose modifiability was unspecified:
"Returns a set of keys in this property list where the key and its corresponding value are strings, including distinct keys in the default property list if a key of the same name has not already been found from the main properties list. Properties whose key or value is not of type String are omitted."
"The returned set is not backed by the Properties object. Changes to this Properties are not reflected in the set, or vice versa.".
In Java 11, it is specified to be unmodifiable.
"Returns an unmodifiable set of keys from this property list where the key and its corresponding value are strings, including distinct keys in the default property list if a key of the same name has not already been found from the main properties list. Properties whose key or value is not of type String are omitted."
"The returned set is not backed by this Properties object. Changes to this Properties object are not reflected in the returned set."
The change happened in Java 9. This is related to bug 8059361.
(This is was an incompatible change in the sense that it could break customers' code if they relied on being able to modify the returned set. However, that code was relying on unspecified behavior, so you could argue that it was broken already.)
As i understand correctly, you have unmodifiable set and you are tying to change it here
parameterNames.removeIf(s -> !s.startsWith(seq));
that's why you getting this error/exception.
Set<String> parameterNames = new HashSet<>(properties.stringPropertyNames());
This will create a new Set with same content which can be altered.
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());
I want to create a new column to store an array list in Parse, but I am unable to create the column (without using the dashboard). It needs to be created in the default "User" class. I've tried creating a Parse object in the user class and I tried querying for the column(hoping that if it doesn't find it, it will create it). It needs to be a column that can store an array list. I am not getting any errors in my code so I am unsure what to do next.
My experience is with the .NET API, but I suspect the principle is the same.
Parse will not create a new column simply from a read; you must set a value in at least one instance, and save it to the DB. This will create the column. Previously existing rows will contain "Undefined" for the new column value, and will not contain a key for the column.
My practice has been to derive types for my various ParseObjects. One thing this affords is that I can wrap the check for the key in my property getters, and set a default value if it is missing.
A caveat: (I'm speaking C#-ese here, so you'll have to do a mental translation) When you derive from ParseObject, you decorate the class with a ParseClassName attribute that defines the name for the document type in your database that your class is bound to. However, Parse already has a derived type, ParseUser, and when you derive from that, you must bind to the predefined "_User" class. (This is true for "_Session" and "_Role" also.)
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?
I have a redis set. This set can store 20 members maximum(added withSADD command).
My problem is ; I need to update that members when needed. Members need that modification maximum 10 times for every member. Set members are json as a string. There is only solutoin on my mind nor, get all members update and recreate that set again. But it seems iditoic.
I know there is ZADD sorted set with its score support it seems suitable also I need to update score like data in json , but i just wonder Is there any method updating members in efficient way, or is updating member not acceptable on redis way ?
Note: Redis datastore is using by both node.js and java clients.
Set members themselves are immutable - you can add new members or remove existing ones. That's it.
Given that a set is an unordered collection of unique members, consider the possible outcomes were set members theoretically modifiable when the new value for a member:
is identical to the old value - no change to the set
already exists in the set - equivalent to deleting that member
isn't 1 or 2 - equivalent to deleting the member and adding a new one