赞
踩
1、zadd :添加元素,格式是:zadd zset的key score值 项的值, Score和项可以是多对,score可以是整数,也可以是浮点数,还可以是+inf表示无穷大,-inf表示负无穷大
操作如下:
java示例代码:
- import redis.clients.jedis.Jedis;
-
- import java.util.HashMap;
- import java.util.Map;
-
- /**
- * redis中对于ZSet类型的: zadd 操作示例
- * zadd :添加元素,格式是:zadd zset的key score值 项的值, Score和项可以是多对,score可以是整数,也可以是浮点数,还可以是+inf表示无穷大,-inf表示负无穷大
- */
- public class ZSet_zadd_operation {
- public static void main(String[] args) {
- Jedis jedis = new Jedis("127.0.0.1",6379);
-
- /**
- * 示例1: zadd zset的key score值 项的值, Score和项可以是多对,score可以是整数,也可以是浮点数,还可以是+inf表示无穷大,-inf表示负无穷大
- */
- Long zadd = jedis.zadd("myzset", 10.0, "v1");
- System.out.println("zadd = " + zadd);
-
-
- Map<String,Double> map = new HashMap<>();
- map.put("v2",20.0);
- map.put("v3",30.0);
- Long myzset = jedis.zadd("myzset", map);
- System.out.println("myzset = " + myzset);
- }
- }
2、zrange :获取索引区间内的元素,格式是:zrange zset的key 起始索引 终止索引 (withscore)
操作如下:
java示例代码:
- import redis.clients.jedis.Jedis;
- import redis.clients.jedis.Tuple;
-
- import java.util.Set;
-
- /**
- * redis中对于ZSet类型的: zrange 操作示例
- * zrange :获取索引区间内的元素,格式是:zrange zset的key 起始索引 终止索引 (withscore)
- */
- public class ZSet_zrange_operation {
- public static void main(String[] args) {
- Jedis jedis = new Jedis("127.0.0.1",6379);
-
- /**
- * 示例1: zrange zset的key 起始索引 终止索引 (withscore)
- */
- Set<String> zrange = jedis.zrange("myzset", 0, -1);
- System.out.println("zrange = " + zrange);
-
- Set<Tuple> zrangeWithScores = jedis.zrangeWithScores("myzset", 0, -1);
- System.out.println("zrangeWithScores = " + zrangeWithScores);
- }
- }
3、zrangebyscore :获取分数区间内的元素,格式是:zrangebyscore zset的key 起始score 终止score (withscore),默认是包含端点值的,如果加上"("表示不包含,后面还可以加上limit来限制。
操作如下:
java示例代码:
- /**
- * redis中对于ZSet类型的: zrange 操作示例
- * zrangebyscore :获取分数区间内的元素,格式是:zrangebyscore zset的key 起始score 终止score (withscore),默认是包含端点值的,如果加上"("表示不包含,后面还可以加上limit来限制。
- */
- public class ZSet_zrangebyscore_operation {
-
- public static void main(String[] args) {
- Jedis jedis = new Jedis("127.0.0.1",6379);
-
- /**
- * 示例1: zrangebyscore zset的key 起始score 终止score (withscore),默认是包含端点值的,如果加上"("表示不包含,后面还可以加上limit来限制。
- */
- Set<String> zrangeByScore = jedis.zrangeByScore("myzset", 10.0, 25.0);
- System.out.println("zrangeByScore = " + zrangeByScore);
-
- Set<Tuple> zrangeByScoreWithScores = jedis.zrangeByScoreWithScores("myzset", 10.0, 25.0);
- System.out.println("zrangeByScoreWithScores = " + zrangeByScoreWithScores);
-
- }
- }
4、zrem :删除元素,格式是:zrem zset的key 项的值,项的值可以是多个
操作如下:
java示例代码:
- /**
- * redis中对于ZSet类型的: zrange 操作示例
- * zrem :删除元素,格式是:zrem zset的key 项的值,项的值可以是多个
- */
- public class ZSet_zrem_operation {
- public static void main(String[] args) {
- Jedis jedis = new Jedis("127.0.0.1",6379);
-
- /**
- * zrem zset的key 项的值,项的值可以是多个
- */
- Long zrem = jedis.zrem("myzset", "v1", "v2");
- System.out.println("zrem = " + zrem);
- }
- }
5、zcard :获取集合中元素个数,格式是:zcard zset的key
操作如下:
java示例代码:
- import redis.clients.jedis.Jedis;
-
- /**
- * redis中对于ZSet类型的: zrange 操作示例
- * zcard :获取集合中元素个数,格式是:zcard zset的key
- */
- public class Zset_zcard_operation {
- public static void main(String[] args) {
- Jedis jedis = new Jedis("127.0.0.1",6379);
-
- /**
- * 示例1: zcard zset的key
- */
- Long zcard = jedis.zcard("myzset");
- System.out.println("zcard = " + zcard);
- }
- }
6、zincrby :增减元素的score,格式是:zincrby zset的key 正负数字 项的值
操作如下:
java示例代码:
- import redis.clients.jedis.Jedis;
-
- /**
- * redis中对于ZSet类型的: zrange 操作示例
- * zincrby :增减元素的score,格式是:zincrby zset的key 正负数字 项的值
- */
- public class ZSet_zincrby_operation {
- public static void main(String[] args) {
- Jedis jedis = new Jedis("127.0.0.1",6379);
-
- /**
- * 示例1: zincrby zset的key 正负数字 项的值
- */
- Double zincrby = jedis.zincrby("myzset", 12, "v1");
- System.out.println("zincrby = " + zincrby);
- }
- }
7、zcount : 获取分数区间内元素个数,格式是:zcount zset的key 起始score 终止score
操作如下:
java示例代码:
- import redis.clients.jedis.Jedis;
-
- /**
- * redis中对于ZSet类型的: zrange 操作示例
- * zcount : 获取分数区间内元素个数,格式是:zcount zset的key 起始score 终止score
- */
- public class ZSet_zcount_operation {
- public static void main(String[] args) {
- Jedis jedis = new Jedis("127.0.0.1",6379);
-
- /**
- * 示例1: zcount zset的key 起始score 终止score
- */
- Long zcount = jedis.zcount("myzset", 20.0, 50.0);
- System.out.println("zcount = " + zcount);
- }
- }
8、zrank : 获取项在zset中的索引,格式是:zrank zset的key 项的值
操作如下:
java示例代码:
- import redis.clients.jedis.Jedis;
-
- /**
- * redis中对于ZSet类型的: zrange 操作示例
- * zrank : 获取项在zset中的索引,格式是:zrank zset的key 项的值
- */
- public class ZSet_zrank_operation {
- public static void main(String[] args) {
- Jedis jedis = new Jedis("127.0.0.1",6379);
-
- /**
- * 示例1:zrank zset的key 项的值
- */
- Long zrank = jedis.zrank("myzset", "v2");
- System.out.println("zrank = " + zrank);
-
- Long zrank2 = jedis.zrank("myzset", "v3");
- System.out.println("zrank2 = " + zrank2);
- }
- }
9、zscore :获取元素的分数,格式是:zscore zset的key 项的值,返回项在zset中的score
操作如下:
java示例代码:
- import redis.clients.jedis.Jedis;
-
- /**
- * redis中对于ZSet类型的: zrange 操作示例
- * zscore :获取元素的分数,格式是:zscore zset的key 项的值,返回项在zset中的score
- */
- public class ZSet_zscore_operation {
- public static void main(String[] args) {
- Jedis jedis = new Jedis("127.0.0.1",6379);
-
- /**
- * 示例1:zscore zset的key 项的值,返回项在zset中的score
- */
- Double zscore = jedis.zscore("myzset", "v2");
- System.out.println("zscore = " + zscore);
-
- Double zscore2 = jedis.zscore("myzset", "v4");
- System.out.println("zscore2 = " + zscore2);
-
- Double zscore3 = jedis.zscore("myzset", "v10");
- System.out.println("zscore3 = " + zscore3);
- }
- }
10、zrevrank :获取项在zset中倒序的索引,格式是:zrevrank zset的key 项的值
操作如下:
java示例代码:
- import redis.clients.jedis.Jedis;
-
- /**
- * redis中对于ZSet类型的: zrange 操作示例
- * zrevrank :获取项在zset中倒序的索引,格式是:zrevrank zset的key 项的值
- */
- public class ZSet_zrevrank_operation {
- public static void main(String[] args) {
- Jedis jedis = new Jedis("127.0.0.1",6379);
-
- /**
- * 示例1:zrevrank zset的key 项的值
- */
- Long zrevrank = jedis.zrevrank("myzset", "v2");
- System.out.println("zrevrank = " + zrevrank);
-
- Long zrevrank2 = jedis.zrevrank("myzset", "v1");
- System.out.println("zrevrank2 = " + zrevrank2);
-
- Long zrevrank3 = jedis.zrevrank("myzset", "v6");
- System.out.println("zrevrank3 = " + zrevrank3);
- }
- }
11、zrevrange :获取索引区间内的元素,格式是:zrevrange zset的key 起始索引 终止索引(withscores)
操作如下:
java示例代码:
- import redis.clients.jedis.Jedis;
-
- import java.util.Set;
-
- /**
- * redis中对于ZSet类型的: zrange 操作示例
- * zrevrange :获取索引区间内的元素,格式是:zrevrange zset的key 起始索引 终止索引(withscores)
- */
- public class ZSet_zrevrange_operation {
- public static void main(String[] args) {
- Jedis jedis = new Jedis("127.0.0.1",6379);
-
- /**
- * 示例1:zrevrange zset的key 起始索引 终止索引(withscores)
- */
- Set<String> zrevrange = jedis.zrevrange("myzset", 0, -1);
- System.out.println("zrevrange = " + zrevrange);
- }
- }
12、zrevrangebyscore:获取分数区间内的元素,格式是:zrevrangebyscore zset的key 终止score 起始score(withscores)
操作如下:
java示例代码:
- import redis.clients.jedis.Jedis;
- import redis.clients.jedis.Tuple;
-
- import java.util.Set;
-
- /**
- * redis中对于ZSet类型的: zrange 操作示例
- * zrevrangebyscore:获取分数区间内的元素,格式是:zrevrangebyscore zset的key 终止score 起始score(withscores)
- */
- public class ZSet_zrevrangebyscore_operation {
- public static void main(String[] args) {
- Jedis jedis = new Jedis("127.0.0.1",6379);
-
- /**
- * 示例1:zrevrangebyscore zset的key 终止score 起始score(withscores)
- */
- Set<String> zrevrangeByScore = jedis.zrevrangeByScore("myzset", 35.0, 15.0);
- System.out.println("zrevrangeByScore = " + zrevrangeByScore);
-
- Set<Tuple> zrevrangeByScoreWithScores = jedis.zrevrangeByScoreWithScores("myzset", 35.0, 15.0);
- System.out.println("zrevrangeByScoreWithScores = " + zrevrangeByScoreWithScores);
- }
- }
13、zremrangebyrank:删除索引区间内的元素,格式是:zremrangebyrank zset的key 起始索引 终止索引
操作如下:
java示例代码:
- import redis.clients.jedis.Jedis;
-
- /**
- * redis中对于ZSet类型的: zremrangebyrank 操作示例
- * zremrangebyrank:删除索引区间内的元素,格式是:zremrangebyrank zset的key 起始索引 终止索引
- */
- public class ZSet_zremrangebyrank_operation {
- public static void main(String[] args) {
- Jedis jedis = new Jedis("127.0.0.1",6379);
-
- /**
- * 示例1:zremrangebyrank zset的key 起始索引 终止索引
- */
- Long zremrangeByRank = jedis.zremrangeByRank("myzset", 0, 2);
- System.out.println("zremrangeByRank = " + zremrangeByRank);
- }
- }
14、zremrangebyscore:删除分数区间内的元素,格式是:zremrangebysocre zset的key 起始score 终止score
操作如下:
java示例代码:
- import redis.clients.jedis.Jedis;
-
- /**
- * redis中对于ZSet类型的: zremrangebyrank 操作示例
- * zremrangebyscore:删除分数区间内的元素,格式是:zremrangebysocre zset的key 起始score 终止score
- */
- public class ZSet_zremrangebyscore_operation {
- public static void main(String[] args) {
- Jedis jedis = new Jedis("127.0.0.1",6379);
-
- /**
- * 示例1:zremrangebysocre zset的key 起始score 终止score
- */
- Long zremrangeByScore = jedis.zremrangeByScore("myzset", 30, 40);
- System.out.println("zremrangeByScore = " + zremrangeByScore);
- }
- }
15、zinterstore:交集,格式是:zinterstore dest-key key-count key[key ...][WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
操作如下:
原始数据:
java示例代码:
- import redis.clients.jedis.Jedis;
- import redis.clients.jedis.ZParams;
-
- /**
- * redis中对于ZSet类型的: zinterstore 操作示例
- * zinterstore:交集,格式是:zinterstore dest-key key-count key[key ...][WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
- */
- public class ZSet_zinterstore_operation {
- public static void main(String[] args) {
- Jedis jedis = new Jedis("127.0.0.1",6379);
-
- /**
- * 示例1:zinterstore dest-key key-count key[key ...][WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
- */
- Long zinterstore = jedis.zinterstore("myzset3", "myzset1", "myzset2");
- System.out.println("zinterstore = " + zinterstore);
-
- Long zinterstore1 = jedis.zinterstore("myzset4", new ZParams().aggregate(ZParams.Aggregate.MAX), "myzset1", "myzset2");
- System.out.println("zinterstore1 = " + zinterstore1);
- }
- }
16、zunionstore:并集,格式是:zunionstore dest-key key-count key[key ...][WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
操作如下:
原始数据:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。