Sorted Sets and Jedis
In the previous post we started looking into Jedis API a Java Redis Client.In this post we will look into the Sorted Set(zsets).
Sorted Set works like a Set in the way it doesn’t allow duplicated values. The big difference is that in Sorted Set each element has a score in order to keep the elements sorted.
We can see some commands below:
CODE:
import java.util.HashMap;
import java.util.Map;
import redis.clients.jedis.Jedis;
public class TestJedis {
public static void main(String[] args) {
String key = "mostUsedLanguages";
Jedis jedis = new Jedis("localhost");
//Adding a value with score to the set
jedis.zadd(key,100,"Java");//ZADD
//We could add more than one value in one calling
Map
scoreMembers = new HashMap();
scoreMembers.put(90d, "Python");
scoreMembers.put(80d, "Javascript");
jedis.zadd(key, scoreMembers);
//We could get the score for a member
System.out.println("Number of Java users:" + jedis.zscore(key, "Java"));
//We could get the number of elements on the set
System.out.println("Number of elements:" + jedis.zcard(key));//ZCARD
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/301743/viewspace-735499/
,如需转载,请注明出处,否则将追究法律责任。