I am receiving a SoapObject with 2 properties having the same name. I want to remove one of them from the Object, and I am not sure how do I achieve that.
The purpose is, I actually want to read both the properties like:
soapObject.getProperty("PropertyName");
I have successfully tried using a for loop:
for(int i = 0; i < soapObject.getPropertyCount(); i++)
soapObject.getProperty(atIndex);
But, it doesn't serve my purpose. I need to retrieve them by name. So, how can I retrieve both?
I tried calling the method twice, but it returns the same values twice.
Have you already tried with the for each loop? It allows you to access both the key and, consequently, the value.
Here you can find an example from which you can start:
for (K key : map.keySet()) {
System.out.println(key + "=" + map.get(key));
}
If i have understood the problem statement correctly
you can remove the duplicate soap object by one of the following methods,(but first add it into an arraylist)
Using Iterator
Using LinkedHashSet. A better way (both time
complexity and ease of implementation wise) is to remove duplicates from an ArrayList is to convert it into a Set that does not allow duplicates
Using Java 8 Stream.distinct() You can use the distinct() method from the Stream API
Related
I am receiving a json string, I need to separate data by class properties. The problem is that I can only know part of the key. For example, the key is 12345#666.777, I only know #666.777. Is it possible to somehow use regular expressions or look for values by part of the key?
I would iterate over all the keys in the JSON object as shown here: https://discuss.kotlinlang.org/t/iterating-over-json-properties/1940/2
and check for every key if it contains the partial key I have as a substring using this standard Kotlin method:
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/contains.html
You also need to treat cases where the same substring is present in 2 different keys (if you think that is possible).
In my Android app we use couchbase lite database version 2.8.6
I run three database queries.
One item in where clause.
QueryBuilder.select(
SelectResult.property("id"),
SelectResult.property("timestamp"),
SelectResult.property("rating"))
.from(DataSource.database(database))
.where(Expression.property("type").equalTo(Expression.string(DOC_TYPE)))
In the result I see three items from database printed to a console. As expected. Format here ans below is id|timestamp|rating
4e39f79c-9e11-4aba-9fb6-95d910f46cd9|0|-2147483648
e95646ee-ba3a-4978-b2a8-5383f31be2f1|0|-2147483648
e02d0eb3-6c9b-4942-b43c-a752eefc77a8|1630525956184|2147483647
I add and() condition to where() to get all items where type = 'type' AND rating < 1
QueryBuilder.select(
SelectResult.property("id"),
SelectResult.property("timestamp"),
SelectResult.property("rating"))
.from(DataSource.database(database))
.where(Expression.property("type").equalTo(Expression.string(DOC_TYPE))
.and(Expression.property("rating").lessThan(Expression.intValue(1))
)
Result is as expected as we search everything with rating < 1, third item is filtered out.
4e39f79c-9e11-4aba-9fb6-95d910f46cd9|0|-2147483648
e95646ee-ba3a-4978-b2a8-5383f31be2f1|0|-2147483648
Finally, I want to see records where type = 'type' AND rating < 1 AND timestamp <= 1
QueryBuilder.select(
SelectResult.property("id"),
SelectResult.property("timestamp"),
SelectResult.property("rating"))
.from(DataSource.database(database))
.where(Expression.property("type").equalTo(Expression.string(DOC_TYPE))
.and(Expression.property("rating").lessThan(Expression.intValue(1))
.and(Expression.property("timestamp")).lessThanOrEqualTo (Expression.longValue(1))
)
)
And now the result is really strange as I receive three items form the database. And the third one has timestamp much greater than 1 put into the query.
4e39f79c-9e11-4aba-9fb6-95d910f46cd9|0|-2147483648
e95646ee-ba3a-4978-b2a8-5383f31be2f1|0|-2147483648
e02d0eb3-6c9b-4942-b43c-a752eefc77a8|1630525956184|2147483647
Any ideas how to make it work with multiple and()? If I try to remove the second and() and keep the third one everything works as expected.
After deep investigations I found several problems in my code:
.and() should be called at the end of the "child" condition and not at the "parent" level.
For the conditions like this
val condition1 = Expression.property("type").equalTo(Expression.string(DOC_TYPE))
val condition2 = Expression.property("rating").lessThan(Expression.intValue(1))
val condition3 = Expression.property("timestamp")).lessThanOrEqualTo (Expression.longValue(1))
The correct way is
.where(condition1.and(
condition2.and(
condition3.and(
...
)
)
)
but NOT like I've tried
.where(condition1.and(condition2)
.and(condition3)
.and(...)
)
For the creation of the object I used a code that converts an object to Map<String, Any> before saving to couchbase. Everything seemed to be OK till I realized that Long was converted to Double.
The last point, I have much complicated queries and inside one of them I accidentally used .add() instead of .and(). No comments.
Hope this will help to somebody to save some time.
I would expect that the query would be as you did originally:
.where(condition1.and(condition2)
.and(condition3)
.and(...))
I think there is an additional parenthesis here that seems to be wrong:
.and(Expression.property("timestamp"))
I want to parse JSONObject and get its keys in the order they do represent to me when i receive. I don't want use JSONObject.keys() as this function giving undefined order of json keys and fully unordered,and this is a huge problem right now because I'm stuck in this position and i have to get json object keys.So is there any way to achieve it? Doing researches i never came across to code which can parse json object keys except JSONObject.keys() which giving reverse order.
There is no way to achieve what you want with a JSONObject because JSONObjects are not ordered by definition and you shouldn't rely on the insertion order. If you need something ordered you should look into JSONArray
I have noticed that although JSONArray gives the proper order, the order won't be kept in HashMap, if you're using it for saving the values.
Using LinkedHashMap solves the order issue.
I have a database that has numbers such as '05' & '5', and there are instances where I need to check if they are the same. I'm trying to normalize them when saving them. Can anyone help me in trying to either remove the '0' or add a '0' so I can get a accurately check if they are same?
I think you can compare the integer values of the both.
Integer.valueOf(05)
Integer.valueOf(5)
If you want to remove the 0 in front of 01,02 from a string, you can use a regular expression replaceAll() with the start character ^
string.replaceAll("^0","");
For multiple 0s, you can use *:
string.replaceAll("^0*","");
On the other hand, you can do the following if you want to check if it doesn't contain 0:
if(!string.contains("0"))
Then if you want you can add a 0 before storing into the database:
string.replaceAll("^","0")
You can use Integer.valueOf(String) method
Integer.valueOf('05') = 5 and Integer.valueOf('5') = 5
then you can check your data using this method:
but if you want to do this in sqlite query this is not usefull.
so, before adding them into your db you can do a replace, it's better than adding a zero.
String number_to_add = string.replaceAll("^0*","");
and then insert this into your db ;)
I am new to Parse and i am working on a Project that uses Android PARSE Sdk so i was wondering on how can i make a where query with or condition using the android sdk.
I want to make a query like this [Psuedo code]
Select * from employ where employId is ("1","2","3")
I found this on parse documentation, i don't know if it helps or not.
Edit:
This is what i found on PARSE but its not working
String[] id= {"1","2","3"};
query.whereContainedIn("employId ", Arrays.asList(id));
It returns me an empty list but if i query them one by one i get result... Can anyone tell me whats wrong ?
You can use whereContainedIn to select specific rows. See this post you can get more ideas. https://www.parse.com/questions/different-arrays-and-wherecontainedin-for-android.
List<String> employId = new ArrayList<String>();
employId.add("1"); employId.add("2"); employId.add("2");
query.whereContainedIn("employId", employId);
If you are still not clear. check this https://www.parse.com/docs/android_guide#queries
I have found the solution and i must say its pretty lame of PARSE to not mention this anywhere in there documentation.
The problem was that the values that i was using inwhereContainedIn method were of type String but in reality they were pointers to another table's row.
I was trying to get the values using only there ids[as it is displayed on parse] but instead i had to pass the whole object in order to retrieve them. That was the reason on why it was returning empty list.
The thing is Even though it displays IDs [pointer to object in a table] we cant search using only ID's instead we have to use complete Parse objects if we want to search a table based on Specific Object.