一、前言

集合在我的第一篇blog《详谈redis数据结构》中也大略讲过,集合是一群无序不重复的元素聚集。注意这里的无序和不重复。

2.1 SADD

SADD全称SET ADD
往一个集合内添加一个或多个成员,并返回本次添加成员的数量

System.out.println(conn.sadd("name","Kelvin","Oscar","Lina"));

etcd redis 区别 redis add和set_java


打开redis desktop manager查看集合内的元素

etcd redis 区别 redis add和set_System_02


已添加成功。

2.2 SREM

SREM 全称SET REM
从集合里面移除一个或多个元素,并返回被移除元素的数量
我们将刚刚的name集合中的“Kelvin”移除掉

System.out.println(conn.srem("name","Kelvin"));

移除一位应当返回1

etcd redis 区别 redis add和set_Java_03

查看redis中存储的数据

etcd redis 区别 redis add和set_java_04


"Kelvin"已经被移除。

2.3 SISMEMBER

SISMEMBER全称Set is member
检查元素是否存在于集合内
刚刚的集合中还有“Lina”但已经没了"Kelvin"写行代码检查一下吧

System.out.println(conn.sismember("name","Kelvin"));
System.out.println(conn.sismember("name","Lina"));

检查结果显示“Kelvin”不是“name”的成员,“Lina”是“name”的成员。

etcd redis 区别 redis add和set_etcd redis 区别_05

2.4 SCARD

SCARD全称SET CARD
返回集合内包含的元素数量
刚刚的“name”集合内还有两个元素。

System.out.println(conn.scard("name"));

etcd redis 区别 redis add和set_redis_06


返回2,符合预期。

2.5 SMEMBERS

SMEMBERS全称set members
返回集合包含的所有元素
刚刚的“name”里面还有“Lina”和"Oscar"

System.out.println(conn.smembers("name"));

etcd redis 区别 redis add和set_System_07

2.6 SRANDMMBER

SRANDMMBER全称set random member
从集合里面随机返回一个或多个元素,当传入的数量参数为正,则返回的随机元素不会重复,当传入的数量参数为负时,返回的随机元素可能会出现重复。

//不传入元素数量时,默认返回一个元素
System.out.println(conn.srandmember("name"));
//正数,则返回元素不会重复
System.out.println(conn.srandmember("name",2));
//负数,则返回元素可能重复
System.out.println(conn.srandmember("name",-2));

现在我们将上面的这段代码多运行几次

etcd redis 区别 redis add和set_System_08


etcd redis 区别 redis add和set_etcd redis 区别_09


etcd redis 区别 redis add和set_java_10


最后一次没重复了。说明重复是可能性不是必然性。

2.7 SPOP

SPOP全称set pop
随机地移除集合中的一个或多个元素,并返回被移除的元素
先将之前的“name”集合全部抹掉,然后再重新往“name”里面添加元素

System.out.println(conn.sadd("name","Lina","Oscar","Kelvin","Trump","Obama","Mary"));
System.out.println(conn.smembers("name"));
System.out.println(conn.spop("name",2));
System.out.println(conn.smembers("name"));

etcd redis 区别 redis add和set_System_11

2.8 SMOVE

SMOVE全称SET MOVE
从"name"中移除"Kelvin”,并将“Kelvin”存到“dest-key”集合中。如果移除成功,则命令返回1,否则返回0.

System.out.println(conn.smembers("name"));
System.out.println(conn.smove("name","dest-key","Kelvin"));
System.out.println(conn.smembers("name"));
System.out.println(conn.smembers("dest-key"));

etcd redis 区别 redis add和set_etcd redis 区别_12

2.9 SDIFF(差集)

SDIFF全称set different
返回那些存在于第一个集合却不存在于其他集合的元素

System.out.println(conn.sadd("set-key1","111","222","333","444","555"));
System.out.println(conn.sadd("set-key2","111","aaa","bbb"));
System.out.println(conn.smembers("set-key1"));
System.out.println(conn.smembers("set-key2"));
System.out.println(conn.sdiff("set-key1","set-key2"));

etcd redis 区别 redis add和set_Java_13

2.10 SDIFFSTORE

SDIFFSTORE全称set different store,将存在于第一个集合但并不存在于其他集合中的元素存储到dest-key中

System.out.println(conn.sadd("set-key1","111","222","333","444","555"));
System.out.println(conn.sadd("set-key2","111","aaa","bbb"));
System.out.println(conn.smembers("set-key1"));
System.out.println(conn.smembers("set-key2"));
System.out.println(conn.sdiffstore("dest-key","set-key1","set-key2"));
System.out.println(conn.smembers("dest-key"));

etcd redis 区别 redis add和set_java_14

2.11 SINTER(交集)

SINTER全称set inter
返回那些同时存在于所有集合中的元素

System.out.println(conn.sadd("set-key1","111","222","333","444","555"));
System.out.println(conn.sadd("set-key2","111","aaa","bbb"));
System.out.println(conn.smembers("set-key1"));
System.out.println(conn.smembers("set-key2"));
System.out.println(conn.sinter("set-key1","set-key2"));

etcd redis 区别 redis add和set_redis_15

2.12 SINTERSTORE

SINTERSTORE全称set inter store
将那些同时存在于所有集合的元素存储到dest-key键里面
先将redis清空,然后执行以下操作

System.out.println(conn.sadd("set-key1","111","222","333","444","555"));
System.out.println(conn.sadd("set-key2","111","aaa","bbb"));
System.out.println(conn.smembers("set-key1"));
System.out.println(conn.smembers("set-key2"));
System.out.println(conn.sinterstore("dest-key","set-key1","set-key2"));
System.out.println(conn.smembers("dest-key"));

etcd redis 区别 redis add和set_redis_16

2.13 SUNION

SUNION全称set union
返回那些至少存在于一个集合中的元素

System.out.println(conn.sadd("set-key1","111","222","333","444","555"));
System.out.println(conn.sadd("set-key2","111","aaa","bbb"));
System.out.println(conn.smembers("set-key1"));
System.out.println(conn.smembers("set-key2"));
System.out.println(conn.sunion("set-key1","set-key2"));

etcd redis 区别 redis add和set_Java_17

2.14 SUNIONSTORE

SUNIONSTORE全称set union store
将那些至少存在于一个集合中的元素存储到dest-key里面

System.out.println(conn.sadd("set-key1","111","222","333","444","555"));
System.out.println(conn.sadd("set-key2","111","aaa","bbb"));
System.out.println(conn.smembers("set-key1"));
System.out.println(conn.smembers("set-key2"));
System.out.println(conn.sunionstore("dest-key","set-key1","set-key2"));
System.out.println(conn.smembers("dest-key"));

etcd redis 区别 redis add和set_etcd redis 区别_18

总结

以上14条命令充分显示出了redis的强大和灵活性,并集交集差集的运算使得redis可以在实际开发中充分运用。