Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is order preserved in set when recovering sorted sets using jedis?

Tags:

java

redis

jedis

I use Java Redis client "Jedis".

When getting a sorted set using zrange for example, the client specifies it returns a Set which by definition has no oredering guarantee.

this old question mentions the problem but I have found no reference to whether it is resolved.

Can I do this and know order will be preserved?

Set<String> results = jedisCli.zrange(key, start, end);
MyObject[] requestedOrderedObjects = new MyObject[results.size];
int i = 0;
foreach(String result: results) {
    requestedOrderedObjects[i++] = MyObject.loadFromString(result);
}
return requestedOrderedObjects;

Thank you for any help.

like image 738
Daren Avatar asked Apr 14 '14 09:04

Daren


1 Answers

Order is preserved, check the type of Set jedis returns: it is indeed a SortedSet and it is ordered. You are right: the API doesn't give you the hint it is ordered, but you should not be afraid: it works fine, or all my apps would have incredible bugs.

like image 97
zenbeni Avatar answered Sep 30 '22 23:09

zenbeni