set集合类型详解
set集合类型详解
set中元素不可重复,是无序的
添加
# sadd key value 往某个set里面添加值
# smembers key 罗列set里面的参数
# sismember key value 判断一个值是否在某个set中
127.0.0.1:6379[3]> flushdb
OK
127.0.0.1:6379[3]> sadd myset hello # 添加元素
(integer) 1
127.0.0.1:6379[3]> sadd myset world baby # 添加多个元素
(integer) 2
127.0.0.1:6379[3]> smembers myset # 罗列myset的元素
1) "baby"
2) "world"
3) "hello"
127.0.0.1:6379[3]> sadd myset hello # set无法添加重复值
(integer) 0
127.0.0.1:6379[3]> smembers myset
1) "baby"
2) "world"
3) "hello"
127.0.0.1:6379[3]> sismember myset oh # 判断myset中是否包含oh
(integer) 0
127.0.0.1:6379[3]> sismember myset baby
移除
# srem key vaue 移除数据
127.0.0.1:6379[3]> srem myset hello
(integer) 1
127.0.0.1:6379[3]> smembers myset
1) "baby"
2) "world"
随机获取几个数
# srandmember key number 随机选取几个值
127.0.0.1:6379[3]> smembers myset
1) "baby"
2) "world"
127.0.0.1:6379[3]> sadd myset hahaha
(integer) 1
127.0.0.1:6379[3]> smembers myset
1) "hahaha"
2) "baby"
3) "world"
127.0.0.1:6379[3]> srandmember myset 2 # 随机选取两个数
1) "hahaha"
2) "world"
127.0.0.1:6379[3]> srandmember myset # 随机选取一个数
"hahaha"
随机删除
# spop key 随机弹出一个值
127.0.0.1:6379[3]> smembers myset
1) "hahaha"
2) "baby"
3) "world"
127.0.0.1:6379[3]> spop myset
"baby"
127.0.0.1:6379[3]> spop myset
"world"
127.0.0.1:6379[3]> smembers myset
1) "hahaha"
将一个指定的值移动到另外一个set中
# smove source destination value 将源set中的值移动到指定set
127.0.0.1:6379[3]> flushdb
OK
127.0.0.1:6379[3]> sadd myset hello0
(integer) 1
127.0.0.1:6379[3]> sadd myset hello1
(integer) 1
127.0.0.1:6379[3]> sadd myset hello2
(integer) 1
127.0.0.1:6379[3]> sadd myset hello3
(integer) 1
127.0.0.1:6379[3]> sadd myset2 world0
(integer) 1
127.0.0.1:6379[3]> sadd myset2 world1
(integer) 1
127.0.0.1:6379[3]> smembers myset
1) "hello2"
2) "hello0"
3) "hello3"
4) "hello1"
127.0.0.1:6379[3]> smembers myset2
1) "world1"
2) "world0"
127.0.0.1:6379[3]> smove myset hello myset2
(integer) 0
127.0.0.1:6379[3]> smove myset myset2 hello
(integer) 0
127.0.0.1:6379[3]> smove myset myset2 hello0
(integer) 1
127.0.0.1:6379[3]> smembers myset
1) "hello2"
2) "hello3"
3) "hello1"
127.0.0.1:6379[3]> smembers myset2
1) "hello0"
2) "world1"
3) "world0"
差集、交集和并集:可用于查找共同好友,共同关注等
# sdiff 差集
# sunoin 并集
# sinter 交集
127.0.0.1:6379[3]> flushdb
OK
127.0.0.1:6379[3]> sadd myset a
(integer) 1
127.0.0.1:6379[3]> sadd myset b
(integer) 1
127.0.0.1:6379[3]> sadd myset c
(integer) 1
127.0.0.1:6379[3]> sadd myset1 c
(integer) 1
127.0.0.1:6379[3]> sadd myset1 d
(integer) 1
127.0.0.1:6379[3]> sadd myset1 e
(integer) 1
127.0.0.1:6379[3]> smembers myset
1) "b"
2) "a"
3) "c"
127.0.0.1:6379[3]> smembers myset1
1) "d"
2) "c"
3) "e"
127.0.0.1:6379[3]> smembers myset1
1) "d"
2) "c"
3) "e"
127.0.0.1:6379[3]> sdiff myset1 myset
1) "d"
2) "e"
127.0.0.1:6379[3]> sdiff myset myset1
1) "a"
2) "b"
127.0.0.1:6379[3]> sdiff myset myset1 a
1) "a"
2) "b"
127.0.0.1:6379[3]> sdiff myset myset1 [a]
1) "a"
2) "b"
127.0.0.1:6379[3]> sdiff myset myset1 c
1) "a"
2) "b"
127.0.0.1:6379[3]> sunion myset myset1
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
127.0.0.1:6379[3]> sinter myset myset1
1) "c"