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