set集合类型的操作(无序集合)——redis
Posted reyinever
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了set集合类型的操作(无序集合)——redis相关的知识,希望对你有一定的参考价值。
1. set集合类型的操作(无序集合)
集合具有无序性(没有顺序)、确定性(描述是确定的)、唯一性(没有重复的元素)
1.1. sadd key member [member ...]
语法:sadd key member [member ...]
作用:往集合key中添加元素
返回值:添加元素的个数
例:
127.0.0.1:6379> sadd fruit apple banana pear
(integer) 3
127.0.0.1:6379> sadd num 1 1
(integer) 1
1.2. smembers key
语法:smembers key
作用:返回集合key中所有的元素
例:
127.0.0.1:6379> smembers fruit
1) "pear"
2) "banana"
3) "apple"
127.0.0.1:6379> smembers num
1) "1"
1.3. srandmember key [count]
语法:srandmember key [count]
作用:返回集合key中随机的1个元素
例:
127.0.0.1:6379> smembers letter
1) "d"
2) "g"
3) "c"
4) "e"
5) "a"
127.0.0.1:6379> srandmember letter
"e"
127.0.0.1:6379> srandmember letter
"c"
127.0.0.1:6379> srandmember letter
"a"
127.0.0.1:6379> srandmember letter 2
1) "c"
2) "e"
127.0.0.1:6379> srandmember letter 3
1) "c"
2) "e"
3) "a"
127.0.0.1:6379> smembers letter
1) "d"
2) "g"
3) "c"
4) "e"
5) "a"
1.4. srem key member [member ...]
语法:srem key member [member ...]
作用:删除集合key中的member [member...]元素
返回值:忽略不存在的元素后,真正删除掉的元素的个数
例:
127.0.0.1:6379> smembers num
1) "1"
127.0.0.1:6379> srem num 1
(integer) 1
127.0.0.1:6379> smembers num
(empty list or set)
127.0.0.1:6379> srem fruit pear
(integer) 1
127.0.0.1:6379> smembers fruit
1) "banana"
2) "apple"
1.5. spop key [count]
语法:spop key [count]
作用:返回并删除集合key中1个随机的元素
例:
127.0.0.1:6379> sadd letter a b c d e f g
(integer) 7
127.0.0.1:6379> smembers letter
1) "c"
2) "d"
3) "g"
4) "b"
5) "e"
6) "a"
7) "f"
127.0.0.1:6379> spop letter
"b"
127.0.0.1:6379> smembers letter
1) "d"
2) "g"
3) "c"
4) "e"
5) "a"
6) "f"
127.0.0.1:6379> spop letter
"f"
127.0.0.1:6379> smembers letter
1) "d"
2) "g"
3) "c"
4) "e"
5) "a"
案例:
spop是返回并删除一个随机的元素,所以可以用来做抽奖,第一次spop出的可以作为第一名(一等奖),第二次spop出的可以作为第二名(二等奖)...
1.6. sismember key member
语法:sismember key member
作用:判断member是否是集合key中的元素
返回值:是返回1;不是返回0
例:
127.0.0.1:6379> smembers letter
1) "d"
2) "g"
3) "c"
4) "e"
5) "a"
127.0.0.1:6379> sismember letter a
(integer) 1
127.0.0.1:6379> sismember letter k
(integer) 0
127.0.0.1:6379> sismember letter 0
(integer) 0
1.7. scard key
语法:scard key
作用:返回集合key中元素的个数
例:
127.0.0.1:6379> smembers letter
1) "d"
2) "g"
3) "c"
4) "e"
5) "a"
127.0.0.1:6379> scard letter
(integer) 5
1.8. sinter key [key ...]
语法:sinter key [key ...]
作用:求出key [key ...]几个集合中的交集,并返回
例:
127.0.0.1:6379> sadd lisa c python java
(integer) 3
127.0.0.1:6379> sadd wang english python php
(integer) 3
127.0.0.1:6379> sadd poly python redis c++
(integer) 3
127.0.0.1:6379> sinter lisa wang poly
1) "python"
127.0.0.1:6379> sadd tom python java php redis
(integer) 4
127.0.0.1:6379> sinter tom poly
1) "redis"
2) "python"
1.9. sinterstore destination key [key ...]
语法:sinterstore destination key [key ...]
作用:求出key [key ...]几个集合中的交集,并赋给destination或存储在destination中
返回值:返回真正存储到destination中的元素的个数
例:
127.0.0.1:6379> smembers lisa
1) "python"
2) "java"
3) "c"
127.0.0.1:6379> smembers poly
1) "c++"
2) "redis"
3) "python"
127.0.0.1:6379> smembers tom
1) "redis"
2) "php"
3) "python"
4) "java"
127.0.0.1:6379> sinterstore lisa_tom lisa tom
(integer) 2
127.0.0.1:6379> smembers lisa_tom
1) "python"
2) "java"
1.10. sunion key [key ...]
语法:sunion key [key ...]
作用:求出key [key ...]几个集合的并集,并返回
例:
127.0.0.1:6379> sadd lisa c python java
(integer) 3
127.0.0.1:6379> sadd wang english python php
(integer) 3
127.0.0.1:6379> sadd poly python redis c++
(integer) 3
127.0.0.1:6379> sunion lisa wang
1) "python"
2) "c"
3) "java"
4) "php"
5) "english"
127.0.0.1:6379> sadd tom python java php redis
(integer) 4
127.0.0.1:6379> sunion lisa wang tom poly
1) "php"
2) "c"
3) "c++"
4) "redis"
5) "python"
6) "english"
7) "java"
1.11. sdiff key [key ...]
语法:sdiff key [key ...]
作用:求出key与[key ...]的差集
例:
127.0.0.1:6379> smembers lisa
1) "python"
2) "java"
3) "c"
127.0.0.1:6379> smembers tom
1) "redis"
2) "php"
3) "python"
4) "java"
127.0.0.1:6379> sdiff lisa tom
1) "c"
127.0.0.1:6379> sdiff tom lisa
1) "redis"
2) "php"
127.0.0.1:6379> smembers poly
1) "c++"
2) "redis"
3) "python"
127.0.0.1:6379> sdiff tom lisa poly
1) "php"
以上是关于set集合类型的操作(无序集合)——redis的主要内容,如果未能解决你的问题,请参考以下文章