Redis中对ZSet类型的操作命令
寫在前面的話:讀書破萬卷,編碼如有神
--------------------------------------------------------------------
?
--------------------------------------------------------------------
1、zadd :添加元素,格式是:zadd zset的key score值 項的值, Score和項可以是多對,score可以是整數(shù),也可以是浮點數(shù),還可以是+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可以是整數(shù),也可以是浮點數(shù),還可以是+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可以是整數(shù),也可以是浮點數(shù),還可以是+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 :獲取索引區(qū)間內的元素,格式是: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 :獲取索引區(qū)間內的元素,格式是: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 :獲取分數(shù)區(qū)間內的元素,格式是:zrangebyscore zset的key 起始score 終止score (withscore),默認是包含端點值的,如果加上"("表示不包含,后面還可以加上limit來限制。
操作如下:
java示例代碼:
1 /**2 * redis中對于ZSet類型的: zrange 操作示例3 * zrangebyscore :獲取分數(shù)區(qū)間內的元素,格式是: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 :獲取集合中元素個數(shù),格式是:zcard zset的key
操作如下:
java示例代碼:
1 import redis.clients.jedis.Jedis;2 3 /**4 * redis中對于ZSet類型的: zrange 操作示例5 * zcard :獲取集合中元素個數(shù),格式是:zcard zset的key6 */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 正負數(shù)字 項的值
操作如下:
java示例代碼:
1 import redis.clients.jedis.Jedis;2 3 /**4 * redis中對于ZSet類型的: zrange 操作示例5 * zincrby :增減元素的score,格式是:zincrby zset的key 正負數(shù)字 項的值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 正負數(shù)字 項的值 13 */ 14 Double zincrby = jedis.zincrby("myzset", 12, "v1"); 15 System.out.println("zincrby = " + zincrby); 16 } 17 }7、zcount : 獲取分數(shù)區(qū)間內元素個數(shù),格式是:zcount zset的key 起始score 終止score
操作如下:
java示例代碼:
1 import redis.clients.jedis.Jedis;2 3 /**4 * redis中對于ZSet類型的: zrange 操作示例5 * zcount : 獲取分數(shù)區(qū)間內元素個數(shù),格式是:zcount zset的key 起始score 終止score6 */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 :獲取元素的分數(shù),格式是:zscore zset的key 項的值,返回項在zset中的score
操作如下:
java示例代碼:
1 import redis.clients.jedis.Jedis;2 3 /**4 * redis中對于ZSet類型的: zrange 操作示例5 * zscore :獲取元素的分數(shù),格式是:zscore zset的key 項的值,返回項在zset中的score6 */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 :獲取索引區(qū)間內的元素,格式是: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 :獲取索引區(qū)間內的元素,格式是: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:獲取分數(shù)區(qū)間內的元素,格式是: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:獲取分數(shù)區(qū)間內的元素,格式是: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:刪除索引區(qū)間內的元素,格式是:zremrangebyrank zset的key 起始索引 終止索引
操作如下:
java示例代碼:
1 import redis.clients.jedis.Jedis;2 3 /**4 * redis中對于ZSet類型的: zremrangebyrank 操作示例5 * zremrangebyrank:刪除索引區(qū)間內的元素,格式是: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:刪除分數(shù)區(qū)間內的元素,格式是:zremrangebysocre zset的key 起始score 終止score
操作如下:
java示例代碼:
1 import redis.clients.jedis.Jedis;2 3 /**4 * redis中對于ZSet類型的: zremrangebyrank 操作示例5 * zremrangebyscore:刪除分數(shù)區(qū)間內的元素,格式是:zremrangebysocre zset的key 起始score 終止score6 */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]
操作如下:
原始數(shù)據(jù):
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]
操作如下:
原始數(shù)據(jù):
?
?
總結
以上是生活随笔為你收集整理的Redis中对ZSet类型的操作命令的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 胆囊炎能不能吃芋头
- 下一篇: Https原理及流程