Redis系列-Redis-Set命令讲解(JAVA-lettuce客户端)
- Redis Set
- SADD key member [member …]
- SCARD key
- SDIFF key [key …]
- SDIFFSTORE destination key [key …]
- SINTER key [key …]
- SINTERSTORE destination key [key …]
- SISMEMBER key member
- SMEMBERS key
- SMOVE source destination member
- SPOP key [count]
- SRANDMEMBER key [count]
- SREM key member [member …]
- SSCAN key cursor [MATCH pattern] [COUNT count]
- SUNION key [key …]
- SUNIONSTORE destination key [key …]
- 源码地址
Redis Set
Redis 的 Set 是 String 类型的无序集合。集合元素是唯一的,这就意味着集合中不能出现重复的元素。
Redis 中集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是 O(1)。以下是 Redis 对 Set 的操作命令:
SADD key member [member …]
将一个或多个元素添加到集合,如果某元素已经存在则忽略此元素,返回添加成功的元素个数。
@Test
public void sadd() {
Assert.assertEquals(2, setCommands.sadd("key-sadd", "a", "b").longValue());
Assert.assertEquals(1, setCommands.sadd("key-sadd", "b", "c").longValue());
}
SCARD key
返回存储在键 key 的集合基数(元素的数量)。
@Test
public void scard() {
setCommands.sadd("key-scard", "a", "b", "c", "c");
Assert.assertEquals(3, setCommands.scard("key-scard").longValue());
}
SDIFF key [key …]
返回从所述第一组和所有的连续组之间的差异而产生的组的成员。
key1 = {a,b,c,d} key2 = {c} key3 = {a,c,e} SDIFF key1 key2 key3 = {b,d}
@Test
public void sdiff() {
setCommands.sadd("key-sdiff-1", "a", "b", "c", "d");
setCommands.sadd("key-sdiff-2", "c");
setCommands.sadd("key-sdiff-3", "a", "c", "e");
Set<String> sdiff = setCommands.sdiff("key-sdiff-1", "key-sdiff-2", "key-sdiff-3");
Assert.assertArrayEquals(new String[]{"b", "d"}, sdiff.toArray(new String[]{}));
}
SDIFFSTORE destination key [key …]
将 SDIFF 返回的结果集存储在键 destination 中,如果键 destination 存在则会被重写。成功返回结果集中元素的数量。
@Test
public void sdiffstore() {
setCommands.sadd("key-sdiffstore-1", "a", "b", "c", "d");
setCommands.sadd("key-sdiffstore-2", "c");
setCommands.sadd("key-sdiffstore-3", "a", "c", "e");
Assert.assertEquals(2, setCommands.sdiffstore("temp", "key-sdiffstore-1", "key-sdiffstore-2", "key-sdiffstore-3").longValue());
Assert.assertArrayEquals(new String[]{"b", "d"}, setCommands.smembers("temp").toArray(new String[]{}));
}
SINTER key [key …]
返回从所有给定集合的交集。
key1 = {a,b,c,d} key2 = {c} key3 = {a,c,e} SINTER key1 key2 key3 = {c}
@Test
public void sinter() {
setCommands.sadd("key-sinter-1", "a", "b", "c", "d");
setCommands.sadd("key-sinter-2", "c");
setCommands.sadd("key-sinter-3", "a", "c", "e");
Set<String> sinter = setCommands.sinter("key-sinter-1", "key-sinter-2", "key-sinter-3");
Assert.assertArrayEquals(new String[]{"c"}, sinter.toArray(new String[]{}));
}
SINTERSTORE destination key [key …]
将 SINTER 返回从所有给定集合的交集存储在键 destination 中,如果键 destination 存在则会被重写。成功返回元结果集中元素的数量。
@Test
public void sinterstore() {
setCommands.sadd("key-sinterstore-1", "a", "b", "c", "d");
setCommands.sadd("key-sinterstore-2", "c");
setCommands.sadd("key-sinterstore-3", "a", "c", "e");
Assert.assertEquals(1, setCommands.sinterstore("temp", "key-sinterstore-1", "key-sinterstore-2", "key-sinterstore-3").longValue());
Assert.assertArrayEquals(new String[]{"c"}, setCommands.smembers("temp").toArray(new String[]{}));
}
SISMEMBER key member
判断键 key 存储的集合是否包含 member 。返回1代表存在该元素,返回0代表不存在该元素或者键 key 不存在。
@Test
public void sismember() {
setCommands.sadd("key-sismember", "a", "b", "c", "d");
Assert.assertTrue(setCommands.sismember("key-sismember", "a"));
Assert.assertFalse(setCommands.sismember("key-sismember", "e"));
}
SMEMBERS key
返回存储在键 key 的所有元素。
@Test
public void smembers() {
Set<String> strings = new HashSet<>(Arrays.asList("a", "b", "c", "d"));
setCommands.sadd("key-smembers", "a", "b", "c", "d");
Set<String> smembers = setCommands.smembers("key-smembers");
for (String smember : smembers) {
Assert.assertTrue(strings.contains(smember));
}
}
SMOVE source destination member
将 member 元素从 source 集合移动到 destination 集合。返回1代表移动成功,返回0代表 source 集合不存在 member 或者 source 不存在。
@Test
public void smove() {
setCommands.sadd("key-smove-1", "a", "b", "c", "d");
setCommands.sadd("key-smove-2", "c");
setCommands.sadd("key-smove-3", "a", "c", "e");
Assert.assertTrue(setCommands.smove("key-smove-1", "key-smove-2", "a"));
Assert.assertFalse(setCommands.smove("key-smove-1", "key-smove-2", "e"));
Assert.assertTrue(setCommands.smove("key-smove-1", "key-smove-3", "c"));
}
SPOP key [count]
该命令类似 SRANDMEMBER 命令,但 SPOP 将随机元素从集合中移除并返回。成功返回取出的元素集合,如果 key 不存在则返回空。
@Test
public void spop() {
setCommands.sadd("key-spop", "a", "b", "c", "d");
Set<String> spop = setCommands.spop("key-spop", 2);
System.out.println(spop);
}
SRANDMEMBER key [count]
随机从 key 集合返回一个或 count 个元素。如果 count 为正数,且小于集合基数,那么命令返回一个包含 count 个元素的数组,数组中的元素各不相同。如果 count 大于等于集合基数,那么返回整个集合。如果 count 为负数,那么命令返回一个数组,数组中的元素可能会重复出现多次,而数组的长度为 count 的绝对值。
@Test
public void srandmember() {
setCommands.sadd("key-srandmember", "a", "b", "c", "d");
List<String> srandmember = setCommands.srandmember("key-srandmember", 2);
System.out.println(srandmember);
}
SREM key member [member …]
从 key 集合删除一个或多个指定的元素。成功返回被删除的元素个数,不存在的元素不计入被删除的个数。
@Test
public void srem() {
setCommands.sadd("key-srem", "a", "b", "c", "d");
Assert.assertEquals(2, setCommands.srem("key-srem", "a", "c", "e").longValue());
}
SSCAN key cursor [MATCH pattern] [COUNT count]
迭代 key 集合中的元素。
@Test
public void sscan() {
setCommands.sadd("key-sscan", "a1", "a2", "a3", "b");
ScanArgs scanArgs = ScanArgs.Builder.matches("a*").limit(1);
ValueScanCursor<String> scanCursor = setCommands.sscan("key-sscan", scanArgs);
System.out.println(scanCursor.getValues());
while (!scanCursor.isFinished()) {
scanCursor = setCommands.sscan("key-sscan", scanCursor, scanArgs);
System.out.println(scanCursor.getValues());
}
}
SUNION key [key …]
返回所有给定集合的并集。
@Test
public void sunion() {
setCommands.sadd("key-sunion-1", "a", "b", "c", "d");
setCommands.sadd("key-sunion-2", "c");
setCommands.sadd("key-sunion-3", "a", "c", "e");
Set<String> sunion = setCommands.sunion("key-sunion-1", "key-sunion-2", "key-sunion-3");
System.out.println(sunion);
}
SUNIONSTORE destination key [key …]
将所有给定集合的并集存储到键 destination 。成功返回集合的元素个数。
@Test
public void sunionstore() {
setCommands.sadd("key-sunionstore-1", "a", "b", "c", "d");
setCommands.sadd("key-sunionstore-2", "c");
setCommands.sadd("key-sunionstore-3", "a", "c", "e");
Assert.assertEquals(5, setCommands.sunionstore("temp", "key-sunionstore-1", "key-sunionstore-2", "key-sunionstore-3").longValue());
}
源码地址
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 rockeycui@163.com
文章标题:Redis系列-Redis-Set命令讲解(JAVA-lettuce客户端)
文章字数:1.7k
本文作者:崔石磊(RockeyCui)
发布时间:2019-11-05, 19:24:47
原始链接:https://cuishilei.com/redis-set.html版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。