set集合中每一个元素都不相同,元素不是按添加顺序排列的(无序的)。
set集合在Redis内部是使用值为空的散列表(hash table)实现的,所以这些操作的时间复杂度都是
0(1)。最方便的是多个集合类型键之间还可以进行并集、交集和差集运算。
以下命令中的key 代表set集合
1.添加元素
sadd key value1 value2 value....
向set中添加元素,set不存在则自动创建,如果元素已经存在于set中,则该元素被忽略,返回成功添加的元素的个数。
localhost:6379> sadd brands samsung iphone huawei sony (integer) 4 localhost:6379> sadd brands samsung (integer) 0
2.删除元素
srem key value1 value2 value...
从集合中删除一个或多个元素,并返回删除成功的个
localhost:6379> sadd brands samsung iphone huawei sony (integer) 4 localhost:6379> srem brands aa bb c (integer) 0 localhost:6379> srem brands samsung (integer) 1
3.获取集合中所有元素
smembers key
localhost:6379> del brands (integer) 1 localhost:6379> sadd brands samsung iphone huawei sony (integer) 4 localhost:6379> smembers brands 1) "huawei" 2) "iphone" 3) "samsung" 4) "sony"
4.判断set中是否存在指定的元素
sismember key value
set集合中是否存在元素值==value的元素,存在则返回1 不存在返回0
localhost:6379> del brands (integer) 1 localhost:6379> sadd brands samsung iphone huawei sony (integer) 4 localhost:6379> sismember brands iphone (integer) 1 localhost:6379> sismember brands lv (integer) 0
5.集合间差运算
sdiff key1 key2 key...
返回:key1集合剔除key2集合中的元素,再剔除key...集合中的元素之后,剩余的元素
计算后:参与计算的集合本身无变化。
localhost:6379> del brands1 (integer) 0 localhost:6379> del brands2 (integer) 0 localhost:6379> sadd brands1 samsung iphone hauwei (integer) 3 localhost:6379> sadd brands2 samsung sony (integer) 2 localhost:6379> sdiff brands2 brands1 1) "sony" localhost:6379> smembers brands2 1) "samsung" 2) "sony"
6.集合间交计算
sinter key1 key2 key....
返回:key1 key2 key.. ,每个集合中都存在的相同元素的合集
计算后:参与计算的集合本身无变化。
localhost:6379> del brands1 (integer) 0 localhost:6379> del brands2 (integer) 0 localhost:6379> sadd brands1 samsung iphone huawei (integer) 3 localhost:6379> sadd brands2 sony samsung htc (integer) 3 localhost:6379> sinter brands1 brands2 1) "samsung" localhost:6379> smembers brands1 1) "huawei" 2) "iphone" 3) "samsung" localhost:6379> smembers brands2 1) "samsung" 2) "sony" 3) "htc"
7.集合间并运算
sunion key1 key key...
返回:key1集合中所有元素+key2集合中所有元素+key...集合中所有元素(由set特性,会剔重)
计算后:参与计算的集合本身无变化。
localhost:6379> del brands1 (integer) 0 localhost:6379> del brands2 (integer) 0 localhost:6379> sadd brands1 samsung iphone huawei (integer) 3 localhost:6379> sadd brands2 sony samsung htc (integer) 3 localhost:6379> sunion brands1 brands2 1) "samsung" 2) "iphone" 3) "huawei" 4) "sony"
8.获取set中元素的个数
scard key
localhost:6379> del brands (integer) 0 localhost:6379> sadd brands samsung iphone sony huawei (integer) 4 localhost:6379> scard brands (integer) 4
9.set间差、交、并运算并存储结果
差运算:
sdiffstore destKey,srcKey1 ,srcKey2 ,srcKey..
srcKey1 集合减去srcKey2 ,再减去srcKey...的结果保存在destKey中
交运算:
sinterstore destKey,srcKey1 ,srcKey2 ,srcKey..
srcKey1 集合,srcKey2集合,srcKey... 集合交运算的结果保存于destKey中
并运算:
sunionstore destKey,srcKey1 ,srcKey2 ,srcKey..
srcKey1 集合,srcKey2集合,srcKey... 集合并运算的结果保存于destKey中
10.随机获取set中的元素
srandmember key [count]
count为可选参数,没有count时只随机获取一个元素,有count,则随机获取count个元素
localhost:6379> del brands (integer) 0 localhost:6379> sadd brands samsung iphone sony huawei (integer) 4 localhost:6379> srandmember brands "iphone" localhost:6379> srandmember brands 2 1) "samsung" 2) "iphone"
11.从集合随机移出一个元素
spop key
localhost:6379> del brands (integer) 0 localhost:6379> sadd brands samsung iphone sony huawei (integer) 4 localhost:6379> spop brands "samsung" localhost:6379> spop brands "sony" localhost:6379> smembers brands 1) "huawei" 2) "iphone"