# 详谈redis之有序集合（ZSET）

## 二、命令

System.out.println(conn.zadd("high",180,"Kelvin"));


#### 2.2 ZREM

ZREM全称zset remove

System.out.println(conn.zrem("high","Kelvin"));


#### 2.3 ZCARD

ZCARD全称zset card

System.out.println(conn.zadd("high",180,"Kelvin"));
System.out.println(conn.zcard("high"));


zcard命令返回3.

#### 2.4 ZINCRBY

ZINCRBY全称zset increase by

System.out.println(conn.zincrby("high",2,"Kelvin"));


#### 2.5 ZCOUNT

ZCOUNT全称zset count

System.out.println(conn.zcount("high",165,190));


#### 2.6 ZRANK

ZRANK全称zset rank

System.out.println(conn.zrank("high","Kelvin"));


#### 2.7 ZSCORE

ZSCORE全称zset score

System.out.println(conn.zscore("high","Kelvin"));


#### 2.8 ZRANGE

ZRANGE全称zset range

//先将high有序集合扩充一下


OK，我们现在选择选出身高排名第3到第6之间的同学，（3,6]

System.out.println(conn.zrange("high",3,6));


System.out.println(conn.zrangeWithScores("high",3,6));


#### 2.9 ZREVRANK

ZREVRANK全称zset reversal rank

System.out.println(conn.zrevrank("high","Yellow"));
System.out.println(conn.zrevrank("high","Kelvin"));


#### 2.10 ZREVRANGE

ZREVRANGE全称zset reverse range

System.out.println(conn.zrevrange("high",1,5));


ZREVRANGEWITHSCORES

System.out.println(conn.zrevrangeWithScores("high",1,5));


#### 2.11 ZRANGEBYSCORE

ZRANGEBYSCORE全称zset range by score

System.out.println(conn.zrangeByScore("high",160,180));


#### 2.12 ZREVRANGEBYSCORE

ZREVRANGEBYSCORE全称zset reversal range by score

System.out.println(conn.zrevrangeByScore("high",180,160));


#### 2.13 ZREMRANGEBYRANK

ZREMRANGEBYRANK全称zset remove range by rank

System.out.println(conn.zremrangeByRank("high",2,4));


#### 2.14 ZREMRANGENYSCORE

ZREMRANGEBYSCORE全称zset remove range by score

System.out.println(conn.zadd("high",174,"Jay"));


System.out.println(conn.zremrangeByScore("high",0,170));


#### 2.15 ZINTERSTORE

ZINTERSTORE全称zset inter store

System.out.println(conn.zadd("zset-key1",1,"a"));


System.out.println(conn.zinterstore("zset-key3","zset-key1","zset-key2"));


ZParams zParams = new ZParams();
zParams.aggregate(ZParams.Aggregate.MAX);
System.out.println(conn.zinterstore("zset-key4",zParams,"zset-key1","zset-key2"));


ZParams zParams = new ZParams();
zParams.aggregate(ZParams.Aggregate.MIN);
System.out.println(conn.zinterstore("zset-key3",zParams,"zset-key1","zset-key2"));


#### 2.16 ZUNIONSTORE

ZUNIONSTORE全称zset union store

ZParams zParams = new ZParams();
zParams.aggregate(ZParams.Aggregate.MAX);
System.out.println(conn.zunionstore("zset-key5",zParams,"zset-key1","zset-key2"));


## 总结

redis的五种数据类型的相关命令已经介绍完毕，接下去还会介绍一些redis中常用的其他命令，比如发布与订阅，键过期时间等等。

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

小新Kelvin

打赏一下，或许你可以加我好友

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
07-04 2506

06-24 1350
03-14 790
12-24 2万+
08-27 731
01-14 935
08-02 4616
08-06 5070